#if defined(TSINGMA_MX) || defined(TSINGMA_GX) || defined(ARCTIC)

/**
   @file sys_usw_acl_api.c

   @date 2015-11-01

   @version v3.0

 */

/****************************************************************************
*
* Header Files
*
****************************************************************************/
#include "ctc_error.h"
#include "ctc_const.h"
#include "ctc_common.h"
#include "ctc_macro.h"
#include "ctc_debug.h"
#include "ctc_hash.h"
#include "ctc_parser.h"
#include "ctc_acl.h"
#include "ctc_linklist.h"
#include "ctc_spool.h"
#include "ctc_field.h"
#include "ctc_acl.h"
#include "ctc_packet.h"

#include "sys_usw_parser_io.h"
#include "sys_usw_acl.h"
#include "sys_usw_ftm.h"
#include "sys_usw_common.h"
#include "sys_usw_wb_common.h"
#include "sys_usw_register.h"
#include "sys_usw_port_api.h"
#include "sys_usw_npm.h"

#include "drv_api.h"

enum sys_acl_key_ltid_type_e
{
    SYS_ACL_KEY_LTID_RSV_BASE=16    ,
    SYS_ACL_KEY_LTID_MAC = SYS_ACL_KEY_LTID_RSV_BASE,  /**< [TMM] ACL MAC key type */
    SYS_ACL_KEY_LTID_IPV4           ,  /**< [TMM] ACL IPv4 key type */
    SYS_ACL_KEY_LTID_IPV6           ,  /**< [TMM] Mode 0: ACL Mac+IPv6 key type, Mode 1: ACL Ipv6 key type */
    SYS_ACL_KEY_LTID_IPV4_EXT       ,  /**< [TMM] ACL IPv4 extend key type */
    SYS_ACL_KEY_LTID_MAC_IPV4       ,  /**< [TMM] ACL Mac+IPv4 key type */
    SYS_ACL_KEY_LTID_MAC_IPV4_EXT   ,  /**< [TMM] ACL Mac+IPv4 extend key type */
    SYS_ACL_KEY_LTID_IPV6_EXT       ,  /**< [TMM] ACL IPv6 extend key type */
    SYS_ACL_KEY_LTID_MAC_IPV6       ,  /**< [TMM] ACL Mac+IPv6 extend key type */
    SYS_ACL_KEY_LTID_CID            ,  /**< [TMM] ACL Category Id key type */
    SYS_ACL_KEY_LTID_INTERFACE      ,  /**< [TMM] ACL Interface key type */
    SYS_ACL_KEY_LTID_FWD            ,  /**< [TMM] ACL Forward key type */
    SYS_ACL_KEY_LTID_FWD_EXT        ,  /**< [TMM] ACL Forward key type */
    SYS_ACL_KEY_LTID_COPP           ,  /**< [TMM] ACL Copp key type */
    SYS_ACL_KEY_LTID_COPP_EXT       ,  /**< [TMM] ACL Copp extend key type */
    SYS_ACL_KEY_LTID_UDF            ,  /**< [TMM] ACL UDF key type */
    SYS_ACL_KEY_LTID_STK_HDR        ,  /**< [TMM] ACL Stacking Header key type */
    SYS_ACL_KEY_LTID_RSV_MAX = SYS_ACL_KEY_LTID_STK_HDR
};

typedef struct sys_acl_tbl_info_s
{
    uint8  block_id;
    uint8  part;
    uint8  op_mode;
    uint32 index;
    void   *buffer[2];
} sys_acl_tbl_info_t;

struct sys_acl_xkey_user_data_s
{
    uint8  udf_data;        /*bit0:[0-15], bit1:[16-31], ...*/
    ctc_xdata_t* xdata;
};
typedef struct sys_acl_xkey_user_data_s sys_acl_xkey_user_data_t;

#define SYS_ACL_HASH_FK_SIZE(max_block_num)     \
    (sizeof(sys_acl_flex_hash_t) + (sizeof(uint16)*max_block_num))
#define SYS_ACL_INVALID_LTID    0
#define SYS_ACL_FAIL_OP(fail, op)   if (fail) {op;}
#define SYS_ACL_MAX_VALUE(VA,VB)        ((VA)>(VB)? (VA): (VB))
#define SYS_ACL_ABS(VA,VB)  ((VA)>(VB) ? ((VA)-(VB)) : ((VB)-(VA)))
#define SYS_ACL_IF_CONTINUE(condition)  \
    if (condition)  {\
        continue;\
    }
#define SYS_ACL_OP_BUILD                0
#define SYS_ACL_OP_READ                 1
#define SYS_ACL_OP_WRITE                2
#define SYS_ACL_OP_DEL                  3
#define SYS_ACL_PRESEL_RSV_NUM          (SYS_ACL_KEY_LTID_RSV_MAX - SYS_ACL_KEY_LTID_RSV_BASE + 1)
#define SYS_ACL_FLEX_KEY_VEC_BLK_NUM    8
#define SYS_ACL_FLEX_KEY_VEC_BLK_SIZE   32
#define SYS_ACL_XLOOKUP_UDF_HIT_IDX_MAX  7

//CTC_MAX_VALUE_CHECK(acl_flex_key->key_type, CTC_ACL_KEY_FLEX_END); need check when key_type > 256

/*add key field to kset bitmap*/
#define SYS_ACL_KSET_ADD_FIELD(field) _sys_tmm_acl_key_field_kset_add(key_type, field, &kset);
#define SYS_ACL_GET_LEAGUE_SIZE(block_id)   (p_usw_acl_master[lchip]->league[block_id].size? p_usw_acl_master[lchip]->league[block_id].size: 1)
#define SYS_ACL_LEAGUE_LINK_BLOCK(block_id, prio) (p_usw_acl_master[lchip]->league[block_id].size > 1 && p_usw_acl_master[lchip]->league[block_id].lkup_level_bitmap == (1<<(prio)))
#define SEL_VALUE(value) (value == -1)?0:value
#define SYS_ACL_BLOCK_PRIO(block_id)    ((block_id>=ACL_IGS_BLOCK_MAX_NUM)?(block_id-ACL_IGS_BLOCK_MAX_NUM):(block_id))
#define SYS_ACL_BLOCK_BASE(block_id)    ((block_id>=ACL_IGS_BLOCK_MAX_NUM)?(ACL_IGS_BLOCK_MAX_NUM):(0))
#define SYS_ACL_BLOCK_DIR(block_id)     ((block_id>=ACL_IGS_BLOCK_MAX_NUM)?(CTC_EGRESS):(CTC_INGRESS))
#define SYS_ACL_BLOCK_MAX_NUM(block_id) ((block_id>=ACL_IGS_BLOCK_MAX_NUM)?(ACL_EGS_BLOCK_MAX_NUM):(ACL_IGS_BLOCK_MAX_NUM))
#define IPFIX_HASH_LKUP_TYPE_UDF_SINGLE 5
#define IPFIX_HASH_LKUP_TYPE_UDF 6

/* Iterate over bits in bit array. */
#define BMP_ITER2(bmp, start, _max, _b)               \
    for ((_b) = start; (_b) < (_max); (_b)++)        \
        if ((bmp)[(_b) / CTC_UINT32_BITS] == 0)        \
            (_b) += (CTC_UINT32_BITS - 1);            \
        else if (CTC_BMP_ISSET((bmp), (_b)))


#define SYS_ACL_ENCODE_PRESEL_TCAM_INDEX(block_id, offset, index)   (index) = ((block_id) * SYS_ACL_PRESEL_TCAM_DEPTH + (offset))
#define SYS_ACL_DECODE_PRESEL_TCAM_INDEX(block_id, offset, index)   \
    do{\
        block_id = index / SYS_ACL_PRESEL_TCAM_DEPTH;\
        offset = index % SYS_ACL_PRESEL_TCAM_DEPTH;\
    }while(0)
#define SYS_ACL_WORD_REVERSE(dst, src, len)    \
{\
    uint32 _temp_data_ = 0;\
    uint16 _loop_ = 0;\
    uint16 _word_num_ = (len)>>2;\
    do {\
        _temp_data_ = ((uint32*)(src))[_loop_];\
        _temp_data_ = ((_temp_data_>>8)&0x00FF00FF)|((_temp_data_<<8)&0xFF00FF00);\
        ((uint32*)(dst))[_loop_] = ((_temp_data_>>16)&0x0000FFFF)|((_temp_data_<<16)&0xFFFF0000);\
    }while(++_loop_ < _word_num_);\
}

#define SYS_ACL_NHID_HASH_NUM 65536

#define SYS_ACL_CHECK_UNION_FLAG_CONFLICT(param, target)\
if((param) && (param) !=(target)){\
    return CTC_E_PARAM_CONFLICT;\
}
#define SYS_ACL_MASK_IS_EMPTY(_mask) sys_usw_acl_array_is_empty((uint8*)_mask, sizeof(_mask))

#define SYS_ACL_SET_FLAG(VAL, FLAG, IS_ADD)\
if(IS_ADD){\
    CTC_SET_FLAG(VAL, FLAG);}\
else{\
    CTC_UNSET_FLAG(VAL, FLAG);}

extern char *ifp_field_str[];

extern int32
sys_usw_acl_map_fwd_type(uint8 pkt_fwd_type_api, uint8* o_pkt_fwd_type);

extern int32
_sys_usw_acl_entry_move_hw_fpa(uint8 lchip, ctc_fpa_entry_t* p_fpa_entry, int32 tcam_idx_new);

extern int32
_sys_usw_acl_map_ip_frag(uint8 lchip, uint8 ctc_ip_frag, uint32* o_frag_info, uint32* o_frag_info_mask);

extern int32
_sys_usw_acl_build_port_info(uint8 lchip, uint8 key_type, ctc_field_key_t* key_field, drv_acl_port_info_t* pport_drv);

extern int32
_sys_usw_acl_check_port_info(uint8 lchip, sys_acl_entry_t* pe, ctc_field_key_t* key_field);
extern int32
_sys_usw_acl_check_discard_type(uint8 discard_type);
extern uint8
_sys_usw_acl_get_league_block(uint8 lchip, uint8 block_id, uint8 *block_list);
extern uint8
_sys_usw_acl_vlan_tag_op_translate_reverse(uint8 lchip, uint8 op_in, uint8 mode_in, uint8* p_op_out);
extern int32
_sys_usw_acl_get_udf_entry_by_priority(uint8 lchip, uint16 priority, sys_acl_udf_entry_t** o_pe_udf);
extern uint8
sys_usw_acl_map_ctc_to_sys_offset_type(uint8 lchip, uint8 offset_type);
extern int32
sys_tmm_acl_add_presel_fields(uint8 lchip, uint16 presel_id, uint8 fields_cnt, ctc_field_key_t *fields);
extern int32
sys_at_map_discard_type(uint8 lchip, uint8 dir, uint8 drop_reason_en);

int32
sys_tmm_acl_presel_with_league(uint8 lchip, uint8 block_id, uint32 lkup_level_bitmap, uint8 size, uint8 is_set, uint8 global_pp_en);
int32
sys_tmm_acl_presel_with_block(uint8 lchip, uint8 block_id, uint8 part_num, uint8 global_pp_en);
int32
sys_tmm_acl_entry_add_presel(uint8 lchip, uint8 block_id, sys_acl_flex_key_t* fk);
int32
sys_tmm_acl_entry_remove_presel(uint8 lchip, uint8 block_id, sys_acl_flex_key_t* fk);
int32
_sys_tmm_acl_block_set_default(uint8 lchip, sys_acl_block_t *pb, uint8 keep_action_pri);
int32
sys_tmm_acl_expand_link_block(uint8 lchip, sys_acl_block_t* pb, sys_acl_flex_key_t* fk, uint8 part_num);
int32
sys_tmm_acl_compress_link_block(uint8 lchip, sys_acl_block_t *pb, sys_acl_flex_key_t* fk);
int32
sys_at_acl_add_hash_nsh_key_field(uint8 lchip, ctc_field_key_t* key_field, sys_acl_hash_entry_t* pe, uint8 is_add);


STATIC int32
_sys_tmm_acl_presel_fpa_block_init(uint8 lchip, uint8 block_id);

int32
_sys_tmm_acl_get_key_field( uint8 lchip, uint32 qid,sys_acl_entry_t* pe, uint32* key_width, uint32* value_array,
                            uint32* mask_array, uint16* offset_dst);

STATIC int32 _sys_tmm_acl_presel_expand_lt_block(uint8 lchip, uint8 block_id, uint8 key_size);
STATIC int32 _sys_tmm_acl_presel_compress_lt_block(uint8 lchip, uint8 block_id, uint8 key_size, uint16 presel_id);

STATIC int32 sys_tmm_set_acl_lkup_pp_enable(uint8 lchip, ctc_acl_league_t* league, uint8* sub_blk, uint8 sub_blk_cnt, uint8 size, uint8  is_add);



#define SYS_SET_HW_MAC(dest, src)     \
    {   \
        (dest)[0]= (((src)[0] << 24) | ((src)[1] << 16) | ((src)[2] << 8) | ((src)[3]));        \
        (dest)[1]= (((src)[4] << 8)  | ((src)[5]));        \
    }

#define SYS_GET_HW_MAC(dest, src)     \
    {   \
        (dest)[3]=  ((src)[0] & 0xff);        \
        (dest)[2]= (((src)[0] >> 8) & 0xff);        \
        (dest)[1]= (((src)[0] >> 16) & 0xff);        \
        (dest)[0]= (((src)[0] >> 24) & 0xff);        \
        (dest)[5]= ((src)[1] & 0xff);        \
        (dest)[4]= (((src)[1] >> 8) & 0xff);        \
    }

#define SYS_SET_HW_IP6(dest, src)     \
    {   \
        (dest)[0]= ((src)[3]);        \
        (dest)[1]= ((src)[2]);        \
        (dest)[2]= ((src)[1]);        \
        (dest)[3]= ((src)[0]);        \
    }


#define SYS_GET_HW_IP6(dest, src)     \
    {   \
        (dest)[3]= ((src)[0]);        \
        (dest)[2]= ((src)[1]);        \
        (dest)[1]= ((src)[2]);        \
        (dest)[0]= ((src)[3]);        \
    }


#define SYS_SET_HW_IP6_LO(dest, src)     \
    {   \
        (dest)[0]= ((src)[3]);        \
        (dest)[1]= ((src)[2]);        \
    }


#define SYS_GET_HW_IP6_LO(dest, src)     \
    {   \
        (dest)[3]= ((src)[0]);        \
        (dest)[2]= ((src)[1]);        \
    }


#define SYS_SET_HW_IP6_HI(dest, src)     \
    {   \
        (dest)[0]= ((src)[1]);        \
        (dest)[1]= ((src)[0]);        \
    }


#define SYS_GET_HW_IP6_HI(dest, src)     \
    {   \
        (dest)[1]= ((src)[0]);        \
        (dest)[0]= ((src)[1]);        \
    }

/*lookup for empty block with same priority*/
#define ACL_MATCH_PB(pb,fg) (!(pb)->valid || ((fg)->priority == (pb)->priority))
#define ACL_KEY_FIELD_IS_SRH(key_type) (key_type == CTC_FIELD_KEY_SRV6_SRH_FLAG || key_type == CTC_FIELD_KEY_SRV6_SRH_TAG ||\
                                        key_type == CTC_FIELD_KEY_SRV6_SRH_NH || key_type == CTC_FIELD_KEY_SRV6_SRH_RT || \
                                        key_type == CTC_FIELD_KEY_SRV6_SRH_SL || key_type == CTC_FIELD_KEY_SRV6_SRH_LE)

extern sys_acl_master_t* p_usw_acl_master[CTC_MAX_LOCAL_CHIP_NUM_PP];

char flex_key_str[] = "FLEX_KEY(100)";

#define SYS_ACL_GLB_TABLE_TYPE_FWD_CTL      0
uint32
_sys_usw_acl_make_ldp_entry(sys_acl_ldp_entry_t* pe_ldp)
{
    return pe_ldp->entry_id;
}

bool
_sys_usw_acl_compare_ldp_entry(sys_acl_ldp_entry_t* pe0_ldp, sys_acl_ldp_entry_t* pe1_ldp)
{
    return(pe0_ldp->entry_id == pe1_ldp->entry_id);
}

uint32
sys_at_acl_get_glb_table(uint8 lchip, uint8 dir, uint8 type)
{
    static uint32 table_map[][CTC_BOTH_DIRECTION] = {
        {IpeFwdAclCtl2_t, EpeAclQosCtl2_t}
    };

    return table_map[type][dir];
}

uint8
sys_tmm_acl_mapping_fpa2key_size(uint8 fpa_size)
{
    switch(fpa_size)
    {
    default:
    case CTC_FPA_KEY_SIZE_80:
        return CTC_ACL_KEY_SIZE_NONE;
    case CTC_FPA_KEY_SIZE_160:
        return CTC_ACL_KEY_SIZE_160;
    case CTC_FPA_KEY_SIZE_320:
        return CTC_ACL_KEY_SIZE_320;
    case CTC_FPA_KEY_SIZE_640:
        return CTC_ACL_KEY_SIZE_480;
    }
    return CTC_ACL_KEY_SIZE_NONE;

}

int32
sys_tmm_acl_dump_fpa_entry(ctc_fpa_entry_t* p_fpa_entry)
{
#ifndef CTC_SHELL_DEBUG_ON
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "entry_id    - %u\n", p_fpa_entry->entry_id);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "priority    - %u\n", p_fpa_entry->priority);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "offset_a    - %u\n", p_fpa_entry->offset_a);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "flag        - %u\n", p_fpa_entry->flag);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "step        - %u\n", p_fpa_entry->step);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "real_step   - %u\n", p_fpa_entry->real_step);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "key_size    - %u\n", p_fpa_entry->key_size);
#endif
    return CTC_E_NONE;
}

int32
sys_tmm_acl_dump_sys_presel(sys_acl_presel_t* presel)
{
#ifndef CTC_SHELL_DEBUG_ON
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "presel_id    - %u\n", presel->presel_id);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "lkup_level   - %u\n", presel->pltb->block_id);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "flag         - %u\n", presel->flag);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "fk           - (sys_acl_flex_key_t*)%p\n", presel->fk);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "buffer       - (sys_acl_buffer_t*)%p\n", presel->buffer);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "\npresel fpa:\n");
#endif   
    sys_tmm_acl_dump_fpa_entry(&presel->fpae);
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_map_key_type_to_ltid(uint8 key_type, uint8 *p_ltid)
{
    uint8  ltid = 0;
    switch(key_type)
    {
    case CTC_ACL_KEY_MAC:
        ltid = SYS_ACL_KEY_LTID_MAC;
        break;

    case CTC_ACL_KEY_IPV4:
        ltid = SYS_ACL_KEY_LTID_IPV4;
        break;

    case CTC_ACL_KEY_IPV4_EXT:
        ltid = SYS_ACL_KEY_LTID_IPV4_EXT;
        break;

    case CTC_ACL_KEY_MAC_IPV4:
        ltid = SYS_ACL_KEY_LTID_MAC_IPV4;
        break;

    case CTC_ACL_KEY_MAC_IPV4_EXT:
        ltid = SYS_ACL_KEY_LTID_MAC_IPV4_EXT;
        break;

    case CTC_ACL_KEY_IPV6:
        ltid = SYS_ACL_KEY_LTID_IPV6;
        break;

    case CTC_ACL_KEY_IPV6_EXT:
        ltid = SYS_ACL_KEY_LTID_IPV6_EXT;
        break;

    case CTC_ACL_KEY_MAC_IPV6:
        ltid = SYS_ACL_KEY_LTID_MAC_IPV6;
        break;

    case CTC_ACL_KEY_CID:
        ltid = SYS_ACL_KEY_LTID_CID;
        break;

    case CTC_ACL_KEY_INTERFACE:
        ltid = SYS_ACL_KEY_LTID_INTERFACE;
        break;

    case CTC_ACL_KEY_FWD:
        ltid = SYS_ACL_KEY_LTID_FWD;
        break;

    case CTC_ACL_KEY_FWD_EXT:
        ltid = SYS_ACL_KEY_LTID_FWD_EXT;
        break;

    case CTC_ACL_KEY_COPP:
        ltid = SYS_ACL_KEY_LTID_COPP;
        break;

    case CTC_ACL_KEY_COPP_EXT:
        ltid = SYS_ACL_KEY_LTID_COPP_EXT;
        break;

    case CTC_ACL_KEY_UDF:
        ltid = SYS_ACL_KEY_LTID_UDF;
        break;

    case CTC_ACL_KEY_STK_HDR:
        ltid = SYS_ACL_KEY_LTID_STK_HDR;
        break;

    default:
        return CTC_E_NOT_SUPPORT;
    }
    *p_ltid = ltid;
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_fk_build_kgen_profile(uint8 lchip, uint8 part, sys_acl_flex_key_t *fk, void *profile)
{
    uint8  loop = 0;
    sys_acl_ext_sel_t* p_sel = &fk->ext_codes[part];

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    sal_memset(profile, 0, sizeof(DsProgramKeyGenProfile_m));
    for (loop = 0; loop < SYS_ACL_EXT_L1_E2_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL0Extractor2b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l1_e2_sel[loop]));
    }

    for (loop = 0; loop < SYS_ACL_EXT_L1_E4_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL0Extractor4b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l1_e4_sel[loop]));
    }

    for (loop = 0; loop < SYS_ACL_EXT_L1_E8_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL0Extractor8b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l1_e8_sel[loop]));
    }


    for (loop = 0; loop < SYS_ACL_EXT_L1_E16_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL0Extractor16b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l1_e16_sel[loop]));
    }

    for (loop = 0; loop < SYS_ACL_EXT_L1_E32_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL0Extractor32b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l1_e32_sel[loop]));
    }

    for (loop = 0; loop < SYS_ACL_EXT_L2_E16_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL1Extractor16b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l2_e16_sel[loop]));
    }

    for (loop = 0; loop < SYS_ACL_EXT_L3_E1_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL2Extractor1b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l3_e1_sel[loop]));
    }

    for (loop = 0; loop < SYS_ACL_EXT_L3_E2_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL2Extractor2b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l3_e2_sel[loop]));
    }

    for (loop = 0; loop < SYS_ACL_EXT_L3_E4_MAX; loop++)
    {
        SetDsProgramKeyGenProfile(V, gL2Extractor4b_0_selFieldId_f + loop, profile, SEL_VALUE(p_sel->l3_e4_sel[loop]));
    }

    return CTC_E_NONE;
}

int32
_sys_tmm_acl_fk_build_presel_ad(uint8 lchip, uint8 part, uint8 ltid, sys_acl_flex_key_t *fk, void* ad)
{
    sys_acl_ext_sel_t* p_sel = &fk->ext_codes[part];
    int8 * xd_sel = fk->xdata_sel;
    uint8  part_num = 0;
    uint8  key_len_type = 0;
    uint8  vni_not_share = 0;
    uint8  l4_port_is_vni = 0;
    uint8  l4_port_is_gre_key = 0;
    uint8  l4_port_is_gre_no_key = 0;
    uint8  macda_is_arp_target = 0;
    uint8  macsa_is_arp_sender = 0;
    uint8  pbm_format_mode = 0;
    uint8  sel_ctl_val = 0;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    part_num = fk->key_size;
    if (part_num == 1)
    {
        key_len_type = 0;
    }
    else if(part_num == 2)
    {
        key_len_type = (part == 1)? 1: 3;
    }
    else if(part_num == 3)
    {
        key_len_type = (part == 1)? 2: 3;
    }
    for(pbm_format_mode = 0; pbm_format_mode < 4; pbm_format_mode++)
    {
        if(CTC_BMP_ISSET(fk->kset.w, SYS_ACL_FIELD_PORT_BITMAP_MODE0+pbm_format_mode))
        {
            break;
        }
    }
    vni_not_share = CTC_BMP_ISSET(fk->kset.w, SYS_ACL_FIELD_VN_ID_SHARE) || CTC_BMP_ISSET(fk->kset.w, SYS_ACL_FIELD_VXLAN_FLAGS_SHARE)? 1: 0;
    l4_port_is_vni = CTC_BMP_ISSET(fk->kset.w, CTC_FIELD_KEY_VN_ID) || CTC_BMP_ISSET(fk->kset.w, CTC_FIELD_KEY_VXLAN_FLAGS)? 1: 0;
    l4_port_is_gre_key = CTC_BMP_ISSET(fk->kset.w, CTC_FIELD_KEY_GRE_KEY) || CTC_BMP_ISSET(fk->kset.w, CTC_FIELD_KEY_NVGRE_KEY)? 1: 0;
    l4_port_is_gre_no_key = CTC_BMP_ISSET(fk->kset.w, CTC_FIELD_KEY_GRE_FLAGS) || CTC_BMP_ISSET(fk->kset.w, CTC_FIELD_KEY_GRE_PROTOCOL_TYPE)? 1: 0;
    macda_is_arp_target = CTC_BMP_ISSET(fk->kset.w, CTC_FIELD_KEY_TARGET_MAC) ? 1: 0;
    macsa_is_arp_sender = CTC_BMP_ISSET(fk->kset.w, CTC_FIELD_KEY_SENDER_MAC) ? 1: 0;
    SetDsLtidSelectAd(V, fs8CategoryIdASel_f, ad, p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_CLASSID_A]);
    SetDsLtidSelectAd(V, fs8CategoryIdBSel_f, ad, p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_CLASSID_B]);
    SetDsLtidSelectAd(V, fs8CategoryIdCSel_f, ad, p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_CLASSID_C]);
    SetDsLtidSelectAd(V, fs8CategoryIdDSel_f, ad, p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_CLASSID_D]);
    SetDsLtidSelectAd(V, ltId_f, ad, ltid);
    sel_ctl_val = p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_UDF_B] > 0 ? p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_UDF_B] : 0;
    SetDsLtidSelectAd(V, udfBFieldsIsCflexHeader_f, ad, sel_ctl_val);
    sel_ctl_val = p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_UDF_A] > 0 ? p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_UDF_A] : 0;
    SetDsLtidSelectAd(V, udfAFieldsIsCflexHeader_f, ad, sel_ctl_val);
    SetDsLtidSelectAd(V, keyLenType_f, ad, key_len_type);

    SetDsLtidSelectAd(V, fs16SrcInfo0Sel_f, ad, 1); /*Use As global port*/
    SetDsLtidSelectAd(V, fs16SrcInfo1Sel_f, ad, p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_FS_1]); /*0:Use As logic port, 1:use as gport */

    SetDsLtidSelectAd(V, fs32PortIndexSel_f, ad, 1); /*Use port bitmap, not logic port*/
    SetDsLtidSelectAd(V, pbmFormatMode_f, ad, pbm_format_mode==3 ? 0 : (pbm_format_mode+1)); /*fix pbm compatiable mode, 0:256, 1:32, 2:64: 3:128*/
    SetDsLtidSelectAd(V, fsSrcInfo0ShareValid_f, ad, 1); /*use port type */
#if 0
    //metadata not share with cid
    if (CTC_ACL_KSET_ISSET(fk->kset, CTC_FIELD_KEY_METADATA))
    {
        SetDsLtidSelectAd(V, fsSrcInfo0IsMetadataShare_f, ad, 1); /*if set use metadata else cid */
    }
    else
    {
        SetDsLtidSelectAd(V, fsSrcInfo0IsMetadataShare_f, ad, 0); /*cid as key*/
    }
#endif
    SetDsLtidSelectAd(V, fsSrcInfo0IsMetadataShare_f, ad, 1); /*if set use metadata else cid */

    SetDsLtidSelectAd(V, fsPrVtagSelectValid_f, ad, 1); /*use port mapped vlan*/

    SetDsLtidSelectAd(V, gArpFsCtl_dMacSel_f, ad, macda_is_arp_target);
    SetDsLtidSelectAd(V, gArpFsCtl_sMacSel_f, ad, macsa_is_arp_sender);


    SetDsLtidSelectAd(V, l2SymmetricSessionEn_f, ad, 0);
    SetDsLtidSelectAd(V, l3l4SymmetricSessionEn_f, ad, 0);

    SetDsLtidSelectAd(V, fs16L4ByteGreShare_f, ad, l4_port_is_gre_key || l4_port_is_gre_no_key);  /*l4 src port As GRE Key*/
    SetDsLtidSelectAd(V, fs16L4ByteGreWithKeyShare_f, ad, l4_port_is_gre_key); /*l4 src port As GRE Key*/
    sel_ctl_val = p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_VNI_SHARE] > 0 ? p_sel->sel_ctl[SYS_ACL_KEXT_CTRL_SEL_VNI_SHARE] : 0;
    SetDsLtidSelectAd(V, fs16L4ByteVxlanShare_f, ad, vni_not_share? sel_ctl_val: l4_port_is_vni); /*l4 src port As VNI*/

    SetDsLtidSelectAd(V, gArpFsCtl_fs16L4ByteShare_f, ad, 1); /*l4 src port As arpCode/ProtocolType*/
    SetDsLtidSelectAd(V, gMplsFsCtl_fs16L4ByteShare_f, ad, 1); /*l4 src port As GRE Key or  MplsLabel2*/


    SetDsLtidSelectAd(V, gArpFsCtl_fs8TosShare_f, ad, 1); /*Tos As isGratuatiusArp/isRarp*/
  //NotSupport  SetDsLtidSelectAd(V, gMplsFsCtl_fs8FieldShare_f, ad, 1); /*Tos/TcpL As L3if*/

    SetDsLtidSelectAd(V, gArpFsCtl_fs2FieldShare_f, ad, 1);  /*ipOption/HdrError As SenderIpChkFail/SrcMacChkFail,
                                                                IpFrag As TargetIpChkFail/DestMacChkFail*/

    SetDsLtidSelectAd(V, gMplsFsCtl_fs2FieldShare_f, ad, 1); /*ipOption/HdrError As mplsLabelNum*/

    SetDsLtidSelectAd(V, gArpFsCtl_fs32DIp0Sel_f, ad, 0); /*Dip0 As TargetIp*/
    SetDsLtidSelectAd(V, gArpFsCtl_fs32SIp0Sel_f, ad, 1);  /*Sip0 As SenderIp*/




    SetDsLtidSelectAd(V, gArpFsCtl_fs32DIp1Sel_f, ad, 3);  /*Dip As HardwareInfo*/
    SetDsLtidSelectAd(V, gArpFsCtl_fs32SIp1Sel_f, ad, 2);  /*Sip As Protocol*/

    SetDsLtidSelectAd(V, gIpv4FsCtl_fs32DIp1Sel_f, ad, 0); /*Dip1 As ipVer*/
    SetDsLtidSelectAd(V, gIpv4FsCtl_fs32SIp1Sel_f, ad, 0); /*Sip1 As Frag*/

    SetDsLtidSelectAd(V, gMplsFsCtl_fs32DIp0Sel_f, ad, 0); /*FwdLabel NotSupport*/
    SetDsLtidSelectAd(V, gMplsFsCtl_fs32SIp0Sel_f, ad, 0); /*CtlWord NotSupport*/



    //  SetDsLtidSelectAd(V, fsMergeDataShareType_f, ad, 0);
    //  SetDsLtidSelectAd(V, fsSrcInfo0ShareType_f, ad, 0);

#if 0
                      DsLtidSelectAd.fsMergeDataShareValid &&
                      ((isVxlan & ProgramTcamAclCtl.fsMergeDataShareTypeCtl(0))
                      || (isGreNoKey & ProgramTcamAclCtl.fsMergeDataShareTypeCtl(1))
                      || (isGreWithKey & ProgramTcamAclCtl.fsMergeDataShareTypeCtl(2)));
#endif

    SetDsLtidSelectAd(V, fsMergeDataShareValid_f, ad, 1);
    SetDsLtidSelectAd(V, disableLookup_f, ad, 0);
    SetDsLtidSelectAd(V, xTableEn_f, ad, 0);
    SetDsLtidSelectAd(V, fs8XPIDataSel0_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_B0]);
    SetDsLtidSelectAd(V, fs8XPIDataSel1_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_B1]);
    SetDsLtidSelectAd(V, fs8XPIDataSel2_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_B2]);
    SetDsLtidSelectAd(V, fs8XPIDataSel3_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_B3]);
    SetDsLtidSelectAd(V, fs16XPIDataSel0_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_W0]);
    SetDsLtidSelectAd(V, fs16XPIDataSel1_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_W1]);
    SetDsLtidSelectAd(V, fs16XPIDataSel2_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_W2]);
    SetDsLtidSelectAd(V, fs16XPIDataSel3_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_W3]);
    SetDsLtidSelectAd(V, fs32XPIDataSel0_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_DW0]);
    SetDsLtidSelectAd(V, fs32XPIDataSel1_f, ad, xd_sel[SYS_ACL_KEXT_XDATA_SEL_DW1]);

    return CTC_E_NONE;
}

#if 0
int32
sys_tmm_acl_del_presel_key(uint8 lchip, uint8 index, uint8 block_id)
{
    uint32 cmd = 0;
    uint16 hw_index = 0;

    SYS_ACL_ENCODE_PRESEL_TCAM_INDEX(block_id, index, hw_index);
    cmd = DRV_IOD(DsLtidSelectTcam_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, hw_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &cmd));
    return CTC_E_NONE;
}
#endif

int32
_sys_tmm_acl_init_vblock(uint8 lchip, uint8 block_id, uint8 part_num, uint8 is_league)
{
    uint8  part = 0;
    uint8  old_part_num = 0;
    uint8  block_valid = 0;
    sys_acl_block_t*    p_acb = &p_usw_acl_master[lchip]->block[block_id];
    ctc_fpa_block_t*    p_vfpab = p_acb->fpab.vblock;

    old_part_num = p_acb[0].fpab.part_num;
    block_valid = is_league ? p_acb[0].valid : 1;
    for (part = 0; part < part_num; part++)
    {
        p_acb[part].valid = block_valid;
        p_acb[part].expand_blk_id = block_id;
        p_acb[part].fpab.part_num = part_num;
        p_acb[part].fpab.base = part* p_acb[0].fpab.entry_count;
        p_acb[part].fpab.part = part;

        if (part < part_num - 1)
        {
            p_acb[part].fpab.next = &(p_acb[part + 1].fpab);
        }

        if (part < old_part_num)
        {
            continue;
        }
        if (part == 1 && old_part_num == 1)  /* 160 broaden to 320 or 480, sync block 1 by block 0 */
        {
            p_acb[1].fpab.start_offset[CTC_FPA_KEY_SIZE_160] = p_acb[0].fpab.start_offset[CTC_FPA_KEY_SIZE_160];
            p_acb[1].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] = p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] ;
            p_acb[1].fpab.sub_free_count[CTC_FPA_KEY_SIZE_160] = p_acb[1].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160];

            p_acb[1].fpab.start_offset[CTC_FPA_KEY_SIZE_320] = p_acb[0].fpab.start_offset[CTC_FPA_KEY_SIZE_320];
            p_acb[1].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320] = p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320] ;
            p_acb[1].fpab.sub_free_count[CTC_FPA_KEY_SIZE_320] = p_acb[1].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320];

            p_acb[1].fpab.start_offset[CTC_FPA_KEY_SIZE_640] = p_acb[0].fpab.start_offset[CTC_FPA_KEY_SIZE_640];
            p_acb[1].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_640] = p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_640];
            p_acb[1].fpab.sub_free_count[CTC_FPA_KEY_SIZE_640] = p_acb[1].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_640];
            if (p_vfpab)
            {
                p_vfpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] = p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]*2;
                p_vfpab->sub_free_count[CTC_FPA_KEY_SIZE_160]  = p_acb[0].fpab.sub_free_count[CTC_FPA_KEY_SIZE_160] + p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160];

                sal_memcpy(&p_vfpab->entries[0], &p_acb[0].fpab.entries[0],
                           sizeof(p_vfpab->entries[0])*p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]);
            }
        }

        if (part == 2)  /* 160 broaden to 480, sync block 2 by block 0 */
        {
            p_acb[2].fpab.start_offset[CTC_FPA_KEY_SIZE_160] = p_acb[0].fpab.start_offset[CTC_FPA_KEY_SIZE_160];
            p_acb[2].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] = p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] + p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320] ;
            p_acb[2].fpab.sub_free_count[CTC_FPA_KEY_SIZE_160] = p_acb[2].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160];

            p_acb[2].fpab.start_offset[CTC_FPA_KEY_SIZE_320] = p_acb[0].fpab.start_offset[CTC_FPA_KEY_SIZE_640];
            p_acb[2].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320] = 0;
            p_acb[2].fpab.sub_free_count[CTC_FPA_KEY_SIZE_320] = 0;

            p_acb[2].fpab.start_offset[CTC_FPA_KEY_SIZE_640] = p_acb[0].fpab.start_offset[CTC_FPA_KEY_SIZE_640];
            p_acb[2].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_640] = p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_640];
            p_acb[2].fpab.sub_free_count[CTC_FPA_KEY_SIZE_640] = p_acb[2].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_640];
            if (p_vfpab)
            {
                p_vfpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] +=  p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] + p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320];
                p_vfpab->sub_free_count[CTC_FPA_KEY_SIZE_160]  +=  p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] + p_acb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320];
            }
        }
    }
    return CTC_E_NONE;
}

#define _ACL_LEAGUE_

int32
sys_tmm_acl_set_league_global_config(uint8 lchip, sys_acl_league_glb_config_t* config)
{
    uint8  loop = 0, part = 0;
    uint8  part_num = 0;
    uint8  dir = SYS_ACL_BLOCK_DIR(config->block_id);
    uint8  acl_prio = SYS_ACL_BLOCK_PRIO(config->block_id);
    uint8  block_base = SYS_ACL_BLOCK_BASE(config->block_id);
    uint32 league_bitmap = 0;
    uint16 cancel_bmp = 0;
    uint32 ctc_league_bitmap = 0;
    uint32 ctc_block_bitmap = 0;
    uint32 cmd = 0;
    uint16 npm_im_en = 0;
    uint8  max_block_num = dir==CTC_INGRESS?ACL_IGS_BLOCK_MAX_NUM:ACL_EGS_BLOCK_MAX_NUM;
    uint32 fid_en = 0;
    uint8 sub_block_cnt = 0;
    uint32 first_priority = 0;
    uint8  block_id = 0;
    ProgramAclLkpSelectTcamSlice_m bitmap;
    ProgramAclTcamSlicePriority_m  priority;
    IpeAclQosCtl_m acl_qos_ctl;
    uint32 tcam_bmp_table[CTC_BOTH_DIRECTION] = {ProgramAclLkpSelectTcamSlice_t, EgrProgramAclLkpSelectTcamSlice_t};
    uint32 tcam_prio_table[CTC_BOTH_DIRECTION] = {ProgramAclTcamSlicePriority_t, EgrProgramAclTcamSlicePriority_t};
    sys_acl_league_t*   p_league = &p_usw_acl_master[lchip]->league[config->block_id];
    sys_acl_block_t*    p_acb = &p_usw_acl_master[lchip]->block[config->block_id];

    SYS_ACL_INIT_CHECK();
    SYS_ACL_DBG_FUNC();
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "block_id     - %u\n", config->block_id);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "type         - %u\n", config->op_type);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "old_part_num - %u\n", config->old_part_num);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "global_pp_en    - %u\n", config->global_pp_en);

    part_num = p_acb->fpab.part_num;
    cmd = DRV_IOR(tcam_bmp_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !config->global_pp_en)), &bitmap));
    cmd = DRV_IOR(tcam_prio_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !config->global_pp_en)), &priority));
    cmd = DRV_IOR(IpeAclQosCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !config->global_pp_en)), &acl_qos_ctl));

    league_bitmap = GetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+acl_prio, &bitmap);
    for (loop=0; p_acb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST && loop<p_league->size; loop++)
    {
        ctc_league_bitmap |= p_league[loop].lkup_level_bitmap;
    }
    ctc_block_bitmap = ((1<<p_acb->fpab.part_num)-1)<<acl_prio;
    if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_UNMERGE)
    {
        ctc_league_bitmap = ctc_block_bitmap;
    }
    if(CTC_INGRESS == dir)
    {
        npm_im_en = GetIpeAclQosCtl(V, mplsInBandOamLevelEn_f, &acl_qos_ctl);
        fid_en = GetIpeAclQosCtl(V, vxlanCompatibilityMode_f, &acl_qos_ctl);
    }

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "part_num     - %u\n", part_num);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "league_bitmap- %u\n", league_bitmap);
    switch(config->op_type)
    {
        case SYS_ACL_LEAGUE_CONFIG_INIT:
        {
            SYS_ACL_IGS_BLK_LOOP(loop, max_block_num)
            {
                SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+loop, &bitmap, 1<<loop);
                SetProgramAclTcamSlicePriority(V, gTcamSlice_0__priority_f+loop, &priority, max_block_num-1-loop);
            }
            break;
        }
        case SYS_ACL_LEAGUE_CONFIG_NPM_IM:
        {
            npm_im_en = config->npm_im_en? (npm_im_en | ctc_league_bitmap): (npm_im_en & (~ctc_league_bitmap));
            SetIpeAclQosCtl(V, mplsInBandOamLevelEn_f, &acl_qos_ctl, npm_im_en);
            break;
        }
        case SYS_ACL_LEAGUE_CONFIG_FID:
        {
            fid_en = config->fid_en? (fid_en | ctc_league_bitmap): (fid_en & (~ctc_league_bitmap));
            SetIpeAclQosCtl(V, vxlanCompatibilityMode_f, &acl_qos_ctl, fid_en);
            break;
        }
        case SYS_ACL_LEAGUE_CONFIG_SET:
        {
            sys_acl_block_t* pb;
            for (block_id=0; block_id<max_block_num; block_id++)
            {
                SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, block_id));
                sub_block_cnt++;
            }
            pb = &SYS_ACL_BLOCK(lchip, config->block_id);
            first_priority = (max_block_num-1);

            for(loop=0; loop < sub_block_cnt; loop++)
            {
                SYS_ACL_PART_LOOP(part, part_num)
                {

                    SetProgramAclTcamSlicePriority(V, gTcamSlice_0__priority_f+pb[part].block_id-block_base, &priority, (first_priority-part*sub_block_cnt));
                    SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+pb[part].block_id-block_base, &bitmap, 0);

                }
                first_priority--;
                pb = pb->next;
            }
            SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+acl_prio, &bitmap, ctc_league_bitmap);
            if (CTC_IS_BIT_SET(npm_im_en, acl_prio))
            {
                SetIpeAclQosCtl(V, mplsInBandOamLevelEn_f, &acl_qos_ctl, (npm_im_en | ctc_league_bitmap));
            }
            if (CTC_IS_BIT_SET(fid_en, acl_prio))
            {
                SetIpeAclQosCtl(V, vxlanCompatibilityMode_f, &acl_qos_ctl, (fid_en | ctc_league_bitmap));
            }
            break;
        }
        case SYS_ACL_LEAGUE_CONFIG_CANCEL:
        {
            uint8 block_id;
            sys_acl_block_t* pb;
            /*caneled block*/
            loop = 0;
            do
            {
                SYS_ACL_PART_LOOP(part, part_num)
                {
                    block_id = config->sub_blk_list[loop]+part;
                    CTC_BIT_SET(cancel_bmp, block_id);
                    SetProgramAclTcamSlicePriority(V, gTcamSlice_0__priority_f+block_id-block_base, &priority, max_block_num-1);
                    SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+block_id-block_base, &bitmap, (1<<block_id));
                }
                loop++;
            }while(loop < config->cancel_sub_blk_cnt);

            /*re-caculate slice priority*/
            first_priority = (max_block_num-1);
            league_bitmap = 0;
            SYS_ACL_PART_LOOP(part, part_num)
            {
                pb = &SYS_ACL_BLOCK(lchip, config->block_id+part);
                while(pb)
                {
                    CTC_BIT_SET(league_bitmap, pb->block_id-block_base);
                    SetProgramAclTcamSlicePriority(V, gTcamSlice_0__priority_f+pb->block_id-block_base, &priority, first_priority);
                    pb = pb->next;
                    first_priority--;
                }
            }
            SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+acl_prio, &bitmap, league_bitmap);
            if (CTC_IS_BIT_SET(npm_im_en, acl_prio))
            {
                SetIpeAclQosCtl(V, mplsInBandOamLevelEn_f, &acl_qos_ctl, ((npm_im_en & (~cancel_bmp))|league_bitmap));
            }
            if (CTC_IS_BIT_SET(fid_en, acl_prio))
            {
                SetIpeAclQosCtl(V, vxlanCompatibilityMode_f, &acl_qos_ctl, ((fid_en & (~cancel_bmp))|league_bitmap));
            }
            break;
        }
        case SYS_ACL_LEAGUE_CONFIG_EXPAND:
        {
            if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_UNMERGE)
            {
                first_priority = GetProgramAclTcamSlicePriority(V, gTcamSlice_0__priority_f+acl_prio+config->old_part_num-1, &priority) -1;
                for (part=config->old_part_num; part<part_num; part++)
                {
                    CTC_BIT_SET(league_bitmap, acl_prio+part);
                    SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+acl_prio+part, &bitmap, 0);
                    SetProgramAclTcamSlicePriority(V, gTcamSlice_0__priority_f+acl_prio+part, &priority, first_priority);
                    first_priority--;
                }
                SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+acl_prio, &bitmap, league_bitmap);
                if (CTC_IS_BIT_SET(npm_im_en, acl_prio))
                {
                    SetIpeAclQosCtl(V, mplsInBandOamLevelEn_f, &acl_qos_ctl, (npm_im_en | ctc_block_bitmap));
                }
                if (CTC_IS_BIT_SET(fid_en, acl_prio))
                {
                    SetIpeAclQosCtl(V, vxlanCompatibilityMode_f, &acl_qos_ctl, (fid_en | ctc_block_bitmap));
                }
            }
            break;
        }
        case SYS_ACL_LEAGUE_CONFIG_COMPRESS:
        {
            ctc_block_bitmap = ((1<<config->old_part_num)-1)<<config->block_id;
            if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_UNMERGE )
            {
                for (part=part_num; part<config->old_part_num; part++)
                {
                    CTC_BIT_UNSET(league_bitmap, acl_prio+part);
                    SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+acl_prio+part, &bitmap, (1 << (acl_prio+part)));
                    SetProgramAclTcamSlicePriority(V, gTcamSlice_0__priority_f+acl_prio+part, &priority, (max_block_num-1));
                }
                SetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+acl_prio, &bitmap, (1<<acl_prio));
                if (CTC_IS_BIT_SET(npm_im_en, acl_prio))
                {
                    SetIpeAclQosCtl(V, mplsInBandOamLevelEn_f, &acl_qos_ctl, ((npm_im_en & (~ctc_block_bitmap))|(league_bitmap)));
                }
                if (CTC_IS_BIT_SET(fid_en, acl_prio))
                {
                    SetIpeAclQosCtl(V, vxlanCompatibilityMode_f, &acl_qos_ctl, ((fid_en & (~ctc_block_bitmap))|(league_bitmap)));
                }
            }
            break;
        }
        default:
            return CTC_E_INVALID_PARAM;
    }
    cmd = DRV_IOW(tcam_bmp_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !config->global_pp_en)), &bitmap));
    cmd = DRV_IOW(tcam_prio_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !config->global_pp_en)), &priority));
    cmd = DRV_IOW(IpeAclQosCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !config->global_pp_en)), &acl_qos_ctl));
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_dump_ctc_league(ctc_acl_league_t* league)
{
#ifndef CTC_SHELL_DEBUG_ON
    if (league!=NULL)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "acl_priority     - %u\n", league->acl_priority);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "dir              - %u\n", league->dir);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "lkup_level_bitmap- 0x%X\n", league->lkup_level_bitmap);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "size             - %u\n", league->size);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "auto_move_en     - %u\n", league->auto_move_en);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "ext_tcam_bitmap  - 0x%X\n", league->ext_tcam_bitmap);
    }
#endif
    return CTC_E_NONE;
}

/*here sub block means the blocks merged*/
STATIC int32
_sys_usw_acl_get_league_sub_blk(uint8 lchip, uint8 op_mode, uint8* sub_blk, uint8 *sub_blk_cnt, uint8* p_size, ctc_acl_league_t* league)
{
    uint8  size = 0;
    //uint8  league_bmp = 0;
    uint32  check_bmp1 = 0;
    uint8  index = 0;
    uint8  block_id = 0;
    uint8  block_base = 0;
    uint8  max_block_num = (league->dir==CTC_INGRESS?ACL_IGS_BLOCK_MAX_NUM:ACL_EGS_BLOCK_MAX_NUM);
    sys_acl_league_t *p_league1 = NULL;
    sys_acl_block_t* pb;

    SYS_ACL_DBG_FUNC();
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "op_mode      - %u\n", op_mode);
    if (p_size)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "size         - %u\n", *p_size);
    }
    _sys_tmm_acl_dump_ctc_league(league);

    block_base = (league->dir==CTC_INGRESS?0:ACL_IGS_BLOCK_MAX_NUM);
    block_id = league->acl_priority+block_base;
    p_league1 = &p_usw_acl_master[lchip]->league[block_id];

    if(op_mode != SYS_ACL_LEAGUE_OP_UNSET)
    {
        size = (league->size == 0) ? 1 : league->size;
        /*check size by prioriy*/
        CTC_MAX_VALUE_CHECK(size+league->acl_priority, max_block_num);
        /*check league mode by priority*/
        #if 0
        check_bmp1 = (1<<block_id) - 1;
        if (check_bmp1 & league->lkup_level_bitmap)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "League mode priority is invalid!\n");
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "check_bmp1:0x%X, lkup_level_bitmap:0x%X\n", check_bmp1, league->lkup_level_bitmap);
            CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
        }
        #endif
        /*check league mode by size*/
        check_bmp1 = (1<<size) - 1;
        if (!CTC_FLAG_ISSET(league->lkup_level_bitmap, check_bmp1<<league->acl_priority))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "FUN: %s() LINE: %d, Block[%d] size check fail!\n", __FUNCTION__, __LINE__, league->acl_priority);
            return CTC_E_INVALID_CONFIG;
        }
    }
    if ((op_mode == SYS_ACL_LEAGUE_OP_SET) || (op_mode == SYS_ACL_LEAGUE_OP_UPDATE))
    {
        uint32 bmp1;
        uint32 bmp2 = p_league1->lkup_level_bitmap;

        size = (league->size == 0) ? 1 : league->size;
        for(index=1; index < size; index++)
        {
            bmp2 |= p_usw_acl_master[lchip]->league[index+block_id].lkup_level_bitmap;
        }

        check_bmp1 = (1<<size) - 1;
        bmp1 = league->lkup_level_bitmap ^ bmp2;
        index = 0;
        do
        {
            if(!CTC_IS_BIT_SET(bmp1, index))
            {
                index++;
                continue;
            }
            if(check_bmp1 != (check_bmp1 & (bmp1>>index)))
            {
                return CTC_E_INVALID_PARAM;
            }

            sub_blk[(*sub_blk_cnt)++] = index + block_base;
            index += size;
        }
        while(index < max_block_num);
    }
    else if(op_mode == SYS_ACL_LEAGUE_OP_CANCEL)
    {
        uint32 bmp1;
        uint32 bmp2 = p_league1->lkup_level_bitmap;

        size = (league->size == 0) ? 1 : league->size;
        for(index=1; index < size; index++)
        {
            bmp2 |= p_usw_acl_master[lchip]->league[index+block_id].lkup_level_bitmap;
        }

        check_bmp1 = (1<<size) - 1;
        bmp1 = league->lkup_level_bitmap ^ bmp2;

        pb = &SYS_ACL_BLOCK(lchip, block_id);
        while(pb->next)
        {
            pb = pb->next;
        }

        while(pb)
        {
            if(!CTC_IS_BIT_SET(bmp1, pb->block_id-block_base))
            {
                break;
            }
            bmp1 &= (~(check_bmp1<<(pb->block_id-block_base)));
            if((check_bmp1 & (bmp2 >> (pb->block_id-block_base))) != check_bmp1)

            {
                return CTC_E_INVALID_PARAM;
            }
            sub_blk[(*sub_blk_cnt)++] = pb->block_id;
            pb = pb->prev;
        }
        if(bmp1 != 0)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Can only cancel the last consecutive block\n");
            return CTC_E_INVALID_PARAM;
        }
    }
    else
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "block_id             - %u\n", block_id);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "lkup_level_bitmap    - 0x%X\n", p_league1->lkup_level_bitmap);

        if (p_size)
        {
            *p_size = p_usw_acl_master[lchip]->league[block_id].size;
        }
        pb = SYS_ACL_BLOCK(lchip, block_id).next;
        while(pb)
        {
            sub_blk[(*sub_blk_cnt)++] = pb->block_id;
            pb = pb->next;
        }
    }

    return CTC_E_NONE;
}

STATIC int32
_sys_usw_acl_check_flex_league(uint8 lchip, uint8* sub_blk, uint8 sub_blk_cnt, ctc_acl_league_t* league, uint8 op_mode)
{
    uint8  index1 = 0, index2 = 0;
    uint8  size = (league->size == 0)? 1: league->size;
    uint8  block_base = (league->dir==CTC_INGRESS?0:ACL_IGS_BLOCK_MAX_NUM);
    uint8  block_id = league->acl_priority+block_base;
    //uint16 chk_bmp2 = 0;
    sys_acl_block_t * pb = &SYS_ACL_BLOCK(lchip, block_id);
    sys_acl_block_t * pb_m = NULL;
    SYS_ACL_DBG_FUNC();

    if (0 != pb->fpab.part)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%d] is in used!\n", block_id);
        return CTC_E_INVALID_CONFIG;
    }
    /*sub block must empty*/
    for (index1=0; index1<size; index1++)
    {
        if (pb->fpab.part_num == 1 && index1 != 0 && !BLOCK_EMPTY(pb, index1))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%d] is in used!\n", league->acl_priority+index1);
            CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
        }

        if(NULL != pb[index1].group_list)
        {
            if(index1 != 0 && !CTC_SLIST_ISEMPTY(pb[index1].group_list))
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%d] is in used for group list!\n", league->acl_priority+index1);
                CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);     
            }
        }

        for (index2=0; index2<sub_blk_cnt; index2++)
        {
            pb_m = &SYS_ACL_BLOCK(lchip, sub_blk[index2]);
            if (!BLOCK_EMPTY(pb_m, index1) || ((op_mode !=SYS_ACL_LEAGUE_OP_CANCEL) && !SYS_ACL_BLOCK_IS_UNMERGED(lchip, sub_blk[index2]+index1)))
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%d] is in used!\n", sub_blk[index2]+index1);
                CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
            }

            
            if(NULL != pb_m[index1].group_list)
            {
                if(!CTC_SLIST_ISEMPTY(pb_m[index1].group_list))
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%d] is in used for group list!\n", sub_blk[index2]+index1);
                    CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);     
                }
            }
        }
    }
    if(op_mode == SYS_ACL_LEAGUE_OP_UPDATE)
    {
        return CTC_E_NONE;
    }

    /*block with entry must in league*/
    if (pb->fpab.part_num > size)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "block entry size larger than league size!\n");
        CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
    }
    else if (pb->fpab.part_num < size)
    {
        for (index1=pb->fpab.part_num; index1 < size; index1++)
        {
            if (!BLOCK_EMPTY(&SYS_ACL_BLOCK(lchip, block_id), index1) || !SYS_ACL_BLOCK_IS_UNMERGED(lchip, block_id+index1))
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%d] is in used!\n", block_id+index1);
                CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
            }
        }
    }

    /*cancel league must empty*/
    if(op_mode == SYS_ACL_LEAGUE_OP_CANCEL)
    {
        uint32 loop;
        uint8  part;

        SYS_ACL_PART_LOOP(part, size)
        {
            if(pb[part].fpab.entry_count == pb[part].fpab.free_count)
            {
                continue;
            }
            for(loop=p_usw_acl_master[lchip]->league[block_id+part].lkup_level_start[sub_blk[sub_blk_cnt-1]+part-block_base]; loop < pb[part].fpab.entry_count; loop++)
            {
                if(NULL != pb[part].fpab.entries[loop])
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Exist entry in the position [%u]\n", loop);
                    return CTC_E_IN_USE;
                }
            }
        }
    }
    return CTC_E_NONE;
}
int32
_sys_tmm_acl_set_league_hw(   uint8 lchip,
                                uint8 block_id,
                                uint8 size,
                                uint8 sub_blk_cnt,
                                uint8* blk_list,
                                uint8 op_type,
                                uint8 global_pp_en)
{
    uint8   part = 0;
    uint8   tmp_size;
    uint8   dir  = SYS_ACL_BLOCK_DIR(block_id);
    uint8   block_base = SYS_ACL_BLOCK_BASE(block_id);
    uint8   max_block_num = SYS_ACL_BLOCK_MAX_NUM(block_id);
    uint32  index = 0;
    uint32  field_val = 0, cmd = 0;
    sys_acl_league_glb_config_t glb_config;
    IpeAclKeySizeCtl_m                  key_size_ctl;
    IpeAclGlobalActionPriorityCtl_m     act_prio_ctl;
    uint32  key_size_table[2] = {IpeAclKeySizeCtl_t, EpeAclKeySizeCtl_t};
    uint32  act_prio_table[2] = {IpeAclGlobalActionPriorityCtl_t, EpeAclGlobalActionPriorityCtl_t};

    sal_memset(&glb_config, 0, sizeof(sys_acl_league_glb_config_t));
    glb_config.block_id = block_id;
    glb_config.op_type = op_type;
    glb_config.cancel_sub_blk_cnt = sub_blk_cnt;
    glb_config.sub_blk_list = blk_list;
    glb_config.global_pp_en = global_pp_en;
    sys_tmm_acl_set_league_global_config(lchip, &glb_config);

    cmd = DRV_IOR(key_size_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &key_size_ctl));
    cmd = DRV_IOR(act_prio_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &act_prio_ctl));

    field_val = GetIpeAclGlobalActionPriorityCtl(V, gLookup_0_actionPriority_f + block_id - block_base, &act_prio_ctl);
    tmp_size = (op_type == SYS_ACL_LEAGUE_CONFIG_SET) ? size : 0;
    for (index=0; index<sub_blk_cnt; index++)
    {
        SetIpeAclKeySizeCtl(V, gAclLookup_0_keySizeType_f + blk_list[index] - block_base, &key_size_ctl, tmp_size);
        SYS_ACL_PART_LOOP(part, size)
        {
            SetIpeAclGlobalActionPriorityCtl(V, gLookup_0_actionPriority_f + blk_list[index] - block_base + part,
                    &act_prio_ctl,
                    (op_type == SYS_ACL_LEAGUE_CONFIG_SET)?field_val:(max_block_num+block_base-blk_list[index]-part));
        }
    }
    cmd = DRV_IOW(act_prio_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &act_prio_ctl));
    cmd = DRV_IOW(key_size_table[dir], DRV_ENTRY_FLAG);

    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &key_size_ctl));
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_set_league_mode(   uint8 lchip,
                                ctc_acl_league_t* league,
                                uint32 new_count,
                                uint8 size,
                                uint8 sub_blk_cnt,
                                uint8* blk_list)
{
    uint8   part                = 0;
    uint8   prev_id             = 0;
    uint8   sub_index           = 0;
    uint32  index               = 0;
    uint32  old_size = 0, new_size = 0;
    uint32  pb_entry_cnt        = 0;
    int32   ret                 = CTC_E_NONE;
    uint8   block_base          = league->dir==CTC_INGRESS?0:ACL_IGS_BLOCK_MAX_NUM;    
    uint8   block_id            = league->acl_priority+block_base;
    uint8   global_pp_en        = league->global_pp_en;
    uint32  lkup_lvl_bmp        = league->lkup_level_bitmap;  
    ctc_fpa_entry_t**   fpa_entrys[3] = {NULL};
    ctc_fpa_entry_t**   vfpa_entrys = NULL;
    sys_acl_league_t*   p_league = &p_usw_acl_master[lchip]->league[block_id];
    sys_acl_league_t*   p_tmp_league = NULL;
    sys_acl_block_t*    p_acb = &p_usw_acl_master[lchip]->block[block_id];
    sys_acl_block_t*    p_tmp_acb = NULL;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "  new_count:%u, sub_blk_cnt:%u\n", new_count, sub_blk_cnt);
    /*rebuild fpa entry addr memory*/
    new_size = sizeof(sys_acl_entry_t*) * new_count;
    for (index=0; index<size; index++)
    {
        MALLOC_ZERO(MEM_ACL_MODULE, fpa_entrys[index], new_size);
        if (NULL == fpa_entrys[index])
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Malloc fpa_entrys[%u], count:%u\n", index, new_count);
            CTC_ERROR_GOTO(CTC_E_NO_MEMORY, ret, free_mem);
        }
    }

    if (p_acb->fpab.vblock && sub_blk_cnt)
    {
        MALLOC_ZERO(MEM_ACL_MODULE, vfpa_entrys, new_size*3);
        if (NULL == vfpa_entrys)
        {
            CTC_ERROR_GOTO(CTC_E_NO_MEMORY, ret, free_mem);
        }
    }

    /*merge fpa entrys addr to new memory*/
    old_size = sizeof(sys_acl_entry_t*) * p_acb->fpab.entry_count;
    for (index=0; index<size; index++)
    {
        sal_memcpy(fpa_entrys[index], p_acb[index].fpab.entries, old_size);
        mem_free(p_acb[index].fpab.entries);
        p_acb[index].fpab.entries = fpa_entrys[index];
    }

    pb_entry_cnt = p_acb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]+p_acb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320]+p_acb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_640];
    /*merge block*/
    for (part = 0; part < size; part++)
    {
        prev_id = block_id + part;
        p_league[part].hdr_block_id = block_id;
        p_league[part].size = size;
        for (index = 0; index < sub_blk_cnt; index++)
        {
            sub_index = blk_list[index] + part;
            p_tmp_league = &p_usw_acl_master[lchip]->league[sub_index];

            _sys_usw_acl_set_league_merge(lchip, &p_league[part], p_tmp_league, 0);
            p_tmp_league->hdr_block_id = block_id;

            p_tmp_acb = &p_usw_acl_master[lchip]->block[prev_id];
            while(p_tmp_acb->next != NULL)
            {
                p_tmp_acb = p_tmp_acb->next;
            }
            p_tmp_acb->next = &p_usw_acl_master[lchip]->block[sub_index];
            p_usw_acl_master[lchip]->block[sub_index].prev = p_tmp_acb;
            prev_id = sub_index;
        }
    }

    /*adjust the 160 part block*/
    if (p_acb->fpab.vblock)
    {
        uint32 old_cnt = 0;
        ctc_fpa_entry_t* pe = NULL;

        if (sub_blk_cnt)
        {
            old_cnt = p_acb->fpab.vblock->entry_count;
            old_size = sizeof(sys_acl_entry_t*) * old_cnt;
            sal_memcpy(vfpa_entrys, p_acb->fpab.vblock->entries, old_size);
            mem_free(p_acb->fpab.vblock->entries);
            p_acb->fpab.vblock->entries = vfpa_entrys;
            p_acb->fpab.vblock->free_count += new_count*3 - p_acb->fpab.vblock->entry_count;
            p_acb->fpab.vblock->entry_count = new_count*3;
        }

        for (index = p_acb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]; index < old_cnt; index++)
        {
            pe = p_acb->fpab.vblock->entries[index];

            if (pe == NULL)
            {
                continue;
            }

            if (pe->offset_a >= (pb_entry_cnt+p_acb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]))
            {
                pe->offset_a += (new_count - pb_entry_cnt)*2;
            }
            else
            {
                pe->offset_a += new_count - pb_entry_cnt;
            }
        }
    }
    for (part=0; part<size; part++)
    {
        p_acb[part].block_status = SYS_ACL_BLOCK_STATUS_MERGE_DST;
        for (index=0; index<sub_blk_cnt; index++)
        {
            p_tmp_acb = &SYS_ACL_BLOCK(lchip, blk_list[index]);
            p_tmp_acb[part].block_status = SYS_ACL_BLOCK_STATUS_MERGE_SRC;
        }
    }
    CTC_ERROR_GOTO(_sys_tmm_acl_set_league_hw(lchip, block_id, size, sub_blk_cnt, blk_list, SYS_ACL_LEAGUE_CONFIG_SET, global_pp_en), ret, free_mem);
    sys_tmm_acl_presel_with_league(lchip, block_id, lkup_lvl_bmp, size, TRUE, global_pp_en);
    return CTC_E_NONE;
free_mem:
    mem_free(vfpa_entrys);
    mem_free(fpa_entrys[2]);
    mem_free(fpa_entrys[1]);
    mem_free(fpa_entrys[0]);
    return ret;
}

int32
_sys_tmm_acl_unset_league_mode( uint8 lchip,
                                uint8 block_id,
                                uint8 size,
                                uint8 sub_blk_cnt,
                                uint8* blk_list,
                                uint8 op_mode,
                                uint8 global_pp_en)
{
    uint16   index = 0;
    uint8   part = 0;
    uint8   tmp_block_id = 0;
    uint8   free_vblock = 1;
    uint8   dir = SYS_ACL_BLOCK_DIR(block_id);
    uint8   block_base = SYS_ACL_BLOCK_BASE(block_id);
    uint8   acl_prio = SYS_ACL_BLOCK_PRIO(block_id);
    uint8   sub_acl_prio = 0;
    uint8   max_block_num = SYS_ACL_BLOCK_MAX_NUM(block_id);
    uint32  cmd = 0;
    uint16  new_size = 0;
    uint16  old_entry_cnt, old_160cnt;
    int32   ret = CTC_E_NONE;
    sys_acl_league_glb_config_t glb_config;
    sys_acl_league_t*   p_league = &p_usw_acl_master[lchip]->league[block_id];
    sys_acl_league_t*   p_tmp_league = NULL;
    sys_acl_block_t*    p_acb = &p_usw_acl_master[lchip]->block[block_id];
    sys_acl_block_t*    p_tmp_acb = NULL;
    ctc_fpa_block_t*    p_vacb = p_usw_acl_master[lchip]->block[block_id].fpab.vblock;
    ctc_fpa_entry_t**   fpa_entrys[3] = {NULL};
    ctc_fpa_entry_t** vacb_entries = NULL;
    IpeAclKeySizeCtl_m                  key_size_ctl;
    IpeAclGlobalActionPriorityCtl_m     act_prio_ctl;
    uint32 key_size_table[CTC_BOTH_DIRECTION] = {IpeAclKeySizeCtl_t, EpeAclKeySizeCtl_t};
    uint32 act_prio_table[CTC_BOTH_DIRECTION] = {IpeAclGlobalActionPriorityCtl_t, EpeAclGlobalActionPriorityCtl_t};
    uint32 canceled_bmp = 0;

    SYS_ACL_DBG_FUNC();
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "block_id:%u\n", block_id);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "size:%u\n", size);
    for (part=0; part<sub_blk_cnt; part++)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "sub_blk[%u]:%u\n", part, blk_list[part]);
    }

    /* all blocks physical entry num are equal. */
    new_size = p_acb->fpab.entry_count - p_acb->entry_num*sub_blk_cnt;
    SYS_ACL_PART_LOOP(part, size)
    {
        MALLOC_ZERO(MEM_ACL_MODULE, fpa_entrys[part], new_size*sizeof(sys_acl_entry_t*));
        if (NULL == fpa_entrys[part])
        {
            CTC_ERROR_GOTO(CTC_E_NO_MEMORY, ret, free_mem);
        }
    }

    if(op_mode == SYS_ACL_LEAGUE_OP_CANCEL && p_vacb)
    {
        MALLOC_ZERO(MEM_ACL_MODULE, vacb_entries, size*new_size*sizeof(sys_acl_entry_t*));
        if (NULL == vacb_entries)
        {
            CTC_ERROR_GOTO(CTC_E_NO_MEMORY, ret, free_mem);
        }
    }
    
    old_entry_cnt = p_acb->fpab.entry_count;
    old_160cnt = p_acb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160];
    for (part = 0; part < size; part++)
    {
        for (index=0; index<sub_blk_cnt; index++)
        {
            tmp_block_id = blk_list[index];

            p_tmp_acb = &p_usw_acl_master[lchip]->block[tmp_block_id];
            p_tmp_acb[part].prev->next = NULL;

            p_tmp_league = &p_usw_acl_master[lchip]->league[tmp_block_id];
            _sys_usw_acl_set_league_cancel(lchip, &p_league[part], &p_tmp_league[part]);
            p_tmp_league[part].hdr_block_id = tmp_block_id + part;
            CTC_BIT_SET(canceled_bmp, tmp_block_id + part - block_base);
        }
    }
    sys_tmm_acl_presel_with_league(lchip, block_id, canceled_bmp, size, FALSE, global_pp_en);

    /*process league & block*/
    SYS_ACL_PART_LOOP(part, size)
    {
        p_league[part].hdr_block_id = (op_mode == SYS_ACL_LEAGUE_OP_CANCEL) ? p_league[part].hdr_block_id : block_id + part;
        p_league[part].size = (op_mode == SYS_ACL_LEAGUE_OP_CANCEL) ? p_league[part].size : 0;
        if (part != 0 && !BLOCK_EMPTY(p_acb, part))
        {
            free_vblock = 0;
        }

        sal_memcpy(fpa_entrys[part], p_acb[part].fpab.entries, new_size*sizeof(sys_acl_entry_t*));
        mem_free(p_acb[part].fpab.entries);
        p_acb[part].fpab.entries = fpa_entrys[part];
        
        if(op_mode != SYS_ACL_LEAGUE_OP_CANCEL)
        {
            p_acb[part].next = NULL;
            p_acb[part].block_status = SYS_ACL_BLOCK_STATUS_UNMERGE;
            p_acb[part].valid = 0;
        }
    }
    if(op_mode == SYS_ACL_LEAGUE_OP_CANCEL && size == 1 && ((1<<acl_prio) == p_usw_acl_master[lchip]->league[block_id].lkup_level_bitmap))
    {
        p_acb->valid = 0;
        p_acb->block_status = 0;
        p_usw_acl_master[lchip]->league[block_id].size = 0;
    }
    /*process vblock*/
    if(op_mode == SYS_ACL_LEAGUE_OP_CANCEL && p_vacb)
    {
        uint16 valid_entries;
        uint16 cnt = 0;
        SYS_ACL_PART_LOOP(part, size)
        {
            if((0 == p_acb[part].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]) ||
                        (p_acb[part].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] == p_acb[part].fpab.sub_free_count[CTC_FPA_KEY_SIZE_160]))
            {
                continue;
            }

            index=0;
            cnt = 0;
            valid_entries = (p_acb[part].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] - p_acb[part].fpab.sub_free_count[CTC_FPA_KEY_SIZE_160]);
            do
            {
                if(p_vacb->entries[index+part*old_160cnt])
                {
                    p_vacb->entries[index+part*old_160cnt]->offset_a -= part*(old_entry_cnt-new_size);
                    vacb_entries[index+part*p_acb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]] = p_vacb->entries[index+part*old_160cnt];
                    cnt++;
                }
                index++;
            }while(cnt < valid_entries && index < p_acb[part].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]);
        }

        p_vacb->free_count = new_size*size - (p_vacb->entry_count-p_vacb->free_count);
        p_vacb->entry_count = new_size*size;

        valid_entries = p_vacb->sub_entry_count[CTC_FPA_KEY_SIZE_160] - p_vacb->sub_free_count[CTC_FPA_KEY_SIZE_160];
        p_vacb->sub_entry_count[CTC_FPA_KEY_SIZE_160] = 0;
        SYS_ACL_PART_LOOP(part, size)
        {
            p_vacb->sub_entry_count[CTC_FPA_KEY_SIZE_160] += p_acb[part].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160];
        }
        p_vacb->sub_free_count[CTC_FPA_KEY_SIZE_160] = p_vacb->sub_entry_count[CTC_FPA_KEY_SIZE_160] - valid_entries;
        mem_free(p_vacb->entries);
        p_vacb->entries = vacb_entries;
    }
    else if ((op_mode != SYS_ACL_LEAGUE_OP_CANCEL) && free_vblock && p_acb->fpab.vblock)
    {
        mem_free(p_acb->fpab.vblock->entries);
        mem_free(p_acb->fpab.vblock);
    }

    sal_memset(&glb_config, 0, sizeof(sys_acl_league_glb_config_t));
    glb_config.block_id = block_id;
    glb_config.op_type = SYS_ACL_LEAGUE_CONFIG_CANCEL;
    glb_config.cancel_sub_blk_cnt = sub_blk_cnt;
    glb_config.sub_blk_list = blk_list;
    glb_config.global_pp_en = global_pp_en;
    sys_tmm_acl_set_league_global_config(lchip, &glb_config);

    cmd = DRV_IOR(key_size_table[dir], DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &key_size_ctl);
    cmd = DRV_IOR(act_prio_table[dir], DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &act_prio_ctl);

    if(op_mode != SYS_ACL_LEAGUE_OP_CANCEL)
    {
        SetIpeAclKeySizeCtl(V, gAclLookup_0_keySizeType_f + acl_prio, &key_size_ctl, 0);
    }
    for (index=0; index<sub_blk_cnt; index++)
    {
        sub_acl_prio = SYS_ACL_BLOCK_PRIO(blk_list[index]);
        SetIpeAclKeySizeCtl(V, gAclLookup_0_keySizeType_f + sub_acl_prio, &key_size_ctl, 0);
        SYS_ACL_PART_LOOP(part, size)
        {
            SetIpeAclGlobalActionPriorityCtl(V, gLookup_0_actionPriority_f + sub_acl_prio + part, &act_prio_ctl, max_block_num-1 - sub_acl_prio - part);
        }
    }
    cmd = DRV_IOW(key_size_table[dir], DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &key_size_ctl);
    cmd = DRV_IOW(act_prio_table[dir], DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &act_prio_ctl);
    return CTC_E_NONE;
free_mem:
    mem_free(fpa_entrys[2]);
    mem_free(fpa_entrys[1]);
    mem_free(fpa_entrys[0]);
    return ret;
}

int32
_sys_tmm_acl_league_link_block(uint8 lchip, ctc_acl_league_t* league, uint8* link_block, uint8 global_pp_en)
{
    uint8  part = 0;
    uint8  league_size = 0;
    uint8  block_empty = TRUE;
    uint8  expand_link_block = 0, compress_link_block = 0;
    uint16 size_to_bitmap = 0;
    //ctc_fpa_block_t* p_vblock = NULL;
    sys_acl_block_t* p_acb = NULL;
    sys_acl_league_t* p_league = NULL;
    uint8  block_base = league->dir==CTC_INGRESS?0:ACL_IGS_BLOCK_MAX_NUM;
    uint8  block_id = league->acl_priority+block_base;

    *link_block = FALSE;
    p_acb = &p_usw_acl_master[lchip]->block[block_id];
    p_league = &p_usw_acl_master[lchip]->league[block_id];
    size_to_bitmap = ((1<<league->size) - 1)<<league->acl_priority;
    expand_link_block = (p_league->size < 2 && league->size > 1 && size_to_bitmap == league->lkup_level_bitmap) ? TRUE : FALSE;
    compress_link_block = (SYS_ACL_LEAGUE_LINK_BLOCK(block_id, league->acl_priority) && (league->lkup_level_bitmap == (1<<league->acl_priority) || league->lkup_level_bitmap == 0)) ? TRUE : FALSE;
    if (expand_link_block == compress_link_block)
    {
        return CTC_E_NONE;
    }
    if (p_league->size > 1)//link block unset league
    {
        for (part = 1; part < p_league->size; part++)
        {
            block_empty = block_empty && BLOCK_EMPTY(p_acb, part);
        }
        if (!block_empty)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%u] have entry size larger than 160!\n", league->acl_priority);
            return (CTC_E_INVALID_CONFIG);
        }
        SYS_ACL_PART_LOOP(part, p_league->size)
        {
            p_acb[part].block_status = SYS_ACL_BLOCK_STATUS_UNMERGE;
            p_league[part].hdr_block_id = league->acl_priority + part + block_base;
        }
        sys_tmm_acl_compress_link_block(lchip, p_acb, NULL);
        
        /* set pp level global acl bmp do this!!! */
        sys_tmm_set_acl_lkup_pp_enable(lchip, league, NULL, 0, p_league->size, 0);
        
        league_size = p_league->size;
        SYS_ACL_PART_LOOP(part, league_size)
        {
            p_league[part].size = 0;
        }

    }
    else    //link block set league
    {
        sys_tmm_acl_expand_link_block(lchip, p_acb, NULL, league->size);
        
        /* set pp level global acl bmp do this!!! */
        sys_tmm_set_acl_lkup_pp_enable(lchip, league, NULL, 0, league->size, 1);
        SYS_ACL_PART_LOOP(part, league->size)
        {
            p_acb[part].block_status = SYS_ACL_BLOCK_STATUS_MERGE_DST;
            p_league[part].hdr_block_id = block_id;
        }
        p_league->size = league->size;
    }

    *link_block = TRUE;
    return CTC_E_NONE;
}

STATIC int32
_sys_usw_acl_check_league_entry_exist(uint8 lchip, uint8 size, uint8 block_id)
{

    uint16 index;
    uint16 start_pos = 0;
    uint16 entry_count = 0;
    uint8  part;
    uint8 block_base = SYS_ACL_BLOCK_BASE(block_id);   
    sys_acl_block_t * pb = NULL;
    sys_acl_block_t * pb_sub = NULL;
    sys_acl_league_t* p_sys_league = &(p_usw_acl_master[lchip]->league[block_id]);

    
    SYS_ACL_PART_LOOP(part, size)
    {
        /* check head block & expand block*/
        pb = &SYS_ACL_BLOCK(lchip, block_id+part);

        if(BLOCK_EMPTY(pb, 0)) 
        {
            continue;
        }
        
        if(block_id != block_id+part)/* expand block*/
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Exist entry in the block[%u]\n", block_id+part);
            return CTC_E_IN_USE;
        }
        else
        {   
            pb_sub = pb->next;/* get first sub block */
            if(pb_sub == NULL)
            {
                return CTC_E_NONE; 
            }

            /* all entries in the main block, so loop index from first sub block to end one for find exist entries */
            start_pos   = p_sys_league[part].lkup_level_start[pb_sub->block_id+part-block_base];
            entry_count = pb[part].fpab.entry_count;

            for(index=start_pos; index<entry_count; index++)
            {
                if(NULL != pb[part].fpab.entries[index])
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Exist entry in the position [%u]\n", index);
                    return CTC_E_IN_USE;
                }
            }

        }

    }

    return CTC_E_NONE;

}
int32
sys_tmm_acl_set_league_mode_flex(uint8 lchip, ctc_acl_league_t* league, uint8* p_sub_blk_cnt, uint8* sub_blk, uint8* p_op_mode)
{
    int32  ret = CTC_E_NONE;
    uint8  size = 0;
    uint8  block_id = 0;
    uint8  block_base = league->dir==CTC_INGRESS?0:ACL_IGS_BLOCK_MAX_NUM;
    uint8  link_block = 0;
    uint8  temp_block = 0;
    uint16 index = 0;
    uint8  part = 0;
    uint8  valid1 = 0, valid2 = 0;
    uint8  op_mode = 0;
    uint8  sub_blk_cnt = 0;
    uint8 old_size = 0;
    uint8 new_size = 0;
    uint32 new_count = 0;
    sys_acl_block_t * pb = NULL;
    sys_acl_block_t * pb_m = NULL;
    sys_acl_league_t* p_sys_league = NULL;
    sys_acl_league_t* p_sys_league_part = NULL;
    sys_acl_lt_block_t * lt_block = NULL;
    uint8   global_pp_en   = league->global_pp_en;
    
    SYS_ACL_DBG_FUNC();

    /*get op mode*/
    block_base = (league->dir == CTC_INGRESS? 0: ACL_IGS_BLOCK_MAX_NUM);
    block_id = league->acl_priority+block_base;
    size = (league->size == 0) ? 1 : league->size;
    p_sys_league = &(p_usw_acl_master[lchip]->league[block_id]);
    old_size = SYS_ACL_GET_LEAGUE_SIZE(block_id);
    lt_block = &SYS_ACL_LT_BLOCK(lchip, block_id);
    
    if (p_sys_league->hdr_block_id != block_id)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block is not hdr block!\n");
        CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
    }

    if(lt_block->fpab.part_num > size || size > CTC_ACL_KEY_SIZE_480)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "lt block size(%d) is more than league size(%d)!\n", lt_block->fpab.part_num, size);
        CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
    }
    
    valid1 = (((1<<league->acl_priority) == league->lkup_level_bitmap) || league->lkup_level_bitmap == 0);
    valid2 = ((1<<league->acl_priority) == p_sys_league->lkup_level_bitmap && p_sys_league->size < CTC_ACL_KEY_SIZE_320);
    if (valid1 && valid2)
    {
        /* set pp level disable acl bmp do this!!! */
        sys_tmm_set_acl_lkup_pp_enable(lchip, league, NULL, 0, size, 1);
        
        return CTC_E_NONE;
    }
    else if (valid1 && !valid2 && p_sys_league->size >= CTC_ACL_KEY_SIZE_320)
    {
        op_mode = SYS_ACL_LEAGUE_OP_UNSET;
    }
    else if (!valid1 && valid2)
    {
        op_mode = SYS_ACL_LEAGUE_OP_SET;
    }
    else
    {
        uint32 bmp = 0;
        
        if (old_size != size)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "League size not match when update, old_size:%d, new_size:%d!\n", old_size, size);
            CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
        }
        SYS_ACL_PART_LOOP(part, size)
        {
            bmp |= p_usw_acl_master[lchip]->league[block_id+part].lkup_level_bitmap;
        }
        if(league->lkup_level_bitmap == bmp) /* not change league status, just to set lkup pp enable */
        {
            /* set pp level global acl bmp do this!!! */
            sys_tmm_set_acl_lkup_pp_enable(lchip, league, NULL, 0, size, 1);

            return CTC_E_NONE;
        }
        op_mode = (league->lkup_level_bitmap == (bmp & league->lkup_level_bitmap)) ? SYS_ACL_LEAGUE_OP_CANCEL : SYS_ACL_LEAGUE_OP_UPDATE;
    }

    if(op_mode == SYS_ACL_LEAGUE_OP_UNSET)
    {
        CTC_ERROR_RETURN(_sys_usw_acl_check_league_entry_exist(lchip, old_size, block_id));
    }
        
    CTC_ERROR_RETURN(_sys_usw_acl_get_league_sub_blk(lchip, op_mode, sub_blk, &sub_blk_cnt, (uint8*)&new_size, league));
        
    if ((op_mode == SYS_ACL_LEAGUE_OP_SET) || (op_mode == SYS_ACL_LEAGUE_OP_UPDATE) || (op_mode == SYS_ACL_LEAGUE_OP_CANCEL))
    {
        CTC_ERROR_RETURN(_sys_usw_acl_check_flex_league(lchip, sub_blk, sub_blk_cnt, league, op_mode));
        for (part=0; part<size; part++)
        {
            /*main block check*/
            pb = &SYS_ACL_BLOCK(lchip, block_id+part);
            p_sys_league_part = &p_usw_acl_master[lchip]->league[block_id+part];
            if ((pb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_SRC) ||
                ((pb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST) && (p_sys_league_part->hdr_block_id != block_id)))
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%d] is merge src or hdr block error!\n", block_id+part);
                CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
            }
            /*sub block check*/
            for (index=0; index<sub_blk_cnt; index++)
            {
                pb_m = &SYS_ACL_BLOCK(lchip, sub_blk[index]+part);
                p_sys_league_part = &p_usw_acl_master[lchip]->league[sub_blk[index]+part];
                if ((pb_m->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST) ||
                    ((pb_m->block_status == SYS_ACL_BLOCK_STATUS_MERGE_SRC) && (p_sys_league_part->hdr_block_id != block_id)))
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Block[%d] is merge dst or hdr block error!\n", sub_blk[index]+part);
                    CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
                }
            }
        }
    }

    pb = &SYS_ACL_BLOCK(lchip, block_id);
    for (temp_block=0; temp_block<ACL_IGS_BLOCK_MAX_NUM; temp_block++)
    {
        new_count += p_sys_league->lkup_level_count[temp_block];
    }

    CTC_ERROR_RETURN(_sys_tmm_acl_league_link_block(lchip, league, &link_block, global_pp_en));
    if (link_block == TRUE)
    {
        return CTC_E_NONE;
    }

    /*update SW table*/
    if ((op_mode == SYS_ACL_LEAGUE_OP_SET) || (op_mode == SYS_ACL_LEAGUE_OP_UPDATE))
    {
        for (index=0; index<sub_blk_cnt; index++)
        {
            pb_m = &SYS_ACL_BLOCK(lchip, sub_blk[index]);
            new_count += pb_m->entry_num;
        }
        sys_tmm_acl_expand_link_block(lchip, pb, NULL, size);
        CTC_ERROR_GOTO(_sys_tmm_acl_set_league_mode(lchip, league, new_count, size, sub_blk_cnt, sub_blk), ret, free_mem);
        /* set pp level global acl bmp do this!!! */
        sys_tmm_set_acl_lkup_pp_enable(lchip, league, sub_blk, sub_blk_cnt, size, 1);

    }
    else if(op_mode == SYS_ACL_LEAGUE_OP_CANCEL)
    {
        CTC_ERROR_RETURN(_sys_tmm_acl_unset_league_mode(lchip, block_id, size, sub_blk_cnt, sub_blk, op_mode, global_pp_en));
        /* set pp level global acl bmp do this!!! */
        sys_tmm_set_acl_lkup_pp_enable(lchip, league, sub_blk, sub_blk_cnt, size, 0);
    }
    else
    { /*unset block */
        CTC_ERROR_RETURN(_sys_tmm_acl_unset_league_mode(lchip, block_id, new_size, sub_blk_cnt, sub_blk, op_mode, global_pp_en));
        sys_tmm_acl_compress_link_block(lchip, pb, NULL);
        /* set pp level global acl bmp do this!!! */
        sys_tmm_set_acl_lkup_pp_enable(lchip, league, sub_blk, sub_blk_cnt, new_size, 0);
    }
    *p_sub_blk_cnt = sub_blk_cnt;
    *p_op_mode = op_mode;
    return CTC_E_NONE;
free_mem:
    if (op_mode == SYS_ACL_LEAGUE_OP_SET && !sub_blk_cnt && pb->fpab.vblock)
    {
        mem_free(pb->fpab.vblock->entries);
        mem_free(pb->fpab.vblock);
    }
    return ret;
}

uint8
sys_tmm_acl_get_league_update_flag(uint8 lchip, uint8 dir, uint8 is_port, uint8 is_vlan, uint8 is_l3if)
{
    uint8  flag = 0;
    uint8  league_update_bmp = 0;
    SYS_USW_ACL_LOCK(lchip);
    league_update_bmp = (dir == CTC_INGRESS) ? p_usw_acl_master[lchip]->league_update_bmp : p_usw_acl_master[lchip]->egs_league_update_bmp;
    if (is_port)
    {
        flag = CTC_IS_BIT_SET(league_update_bmp, SYS_ACL_LEAGUE_UPDATE_PORT);
    }
    else if (is_vlan)
    {
        flag = CTC_IS_BIT_SET(league_update_bmp, SYS_ACL_LEAGUE_UPDATE_VLAN);
    }
    else
    {
        flag = CTC_IS_BIT_SET(league_update_bmp, SYS_ACL_LEAGUE_UPDATE_L3IF);
    }
    SYS_USW_ACL_UNLOCK(lchip);
    return flag;
}

STATIC int32 sys_tmm_set_acl_lkup_pp_enable(uint8 lchip, ctc_acl_league_t* league, uint8* sub_blk, uint8 sub_blk_cnt, uint8 size, uint8  is_add)
{
    uint32  cancel_block_bmp        = 0; 
    uint8   index                   = 0;
    uint8   block_id                = 0;
    uint8   part                    = 0;
    uint8   block_base              = league->dir==CTC_INGRESS?0:ACL_IGS_BLOCK_MAX_NUM;
    uint32  set_block_bmp           = 0; 
    uint8   global_pp_en            = league->global_pp_en;
    uint8   op_mode                 = is_add;
    uint32  *lkup_pp_en_bitmap      = &(p_usw_acl_master[lchip]->global_pp_en_bmp[league->dir]); 

        
    if(!MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_ACL) || lchip != SYS_PP_BASE(lchip))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO,"wrong lchip:%u, dir:%u, block_bmp:0x%08x, is_add:%d\n", lchip, league->dir, league->lkup_level_bitmap, is_add);
        
        return CTC_E_NONE;
    }

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM,"lchip:%u, dir:%u, block_bmp:0x%08x\n", lchip, league->dir, league->lkup_level_bitmap);


    if(global_pp_en)
    {
        if(is_add)
        {
             *lkup_pp_en_bitmap |= league->lkup_level_bitmap;
             set_block_bmp = league->lkup_level_bitmap;
        }
        else
        {
            for (part = 0; part < size; part++)
            {
                for(index=0; index<sub_blk_cnt; index++)
                {
                    block_id = sub_blk[index]+part-block_base;
                    cancel_block_bmp |= 1<<block_id;
                }
            }

            block_id = league->acl_priority+block_base;
            SYS_ACL_PART_LOOP(part, size)
            {
                cancel_block_bmp |= p_usw_acl_master[lchip]->league[block_id+part].lkup_level_bitmap;
            }
            set_block_bmp = cancel_block_bmp;
            if(CTC_IS_BIT_SET(*lkup_pp_en_bitmap, league->acl_priority))
            {
                set_block_bmp = cancel_block_bmp;
                SYS_ACL_PART_LOOP(part, size)
                {
                    CTC_BIT_UNSET(set_block_bmp, league->acl_priority+part);
                }
            }
            else
            {
                set_block_bmp = league->lkup_level_bitmap;
                op_mode = 1;
            }

            *lkup_pp_en_bitmap = (*lkup_pp_en_bitmap & ~cancel_block_bmp) | league->lkup_level_bitmap;
        }
    }
    else
    {
        if(is_add)
        {
            if(*lkup_pp_en_bitmap & league->lkup_level_bitmap)
            {
                *lkup_pp_en_bitmap &= ~(league->lkup_level_bitmap);

                set_block_bmp = league->lkup_level_bitmap;

                op_mode = 0;/* remove pp disable lkup blocks */
            }
            else if(league->lkup_level_bitmap == 0)
            {
                cancel_block_bmp = 1 << league->acl_priority;
                *lkup_pp_en_bitmap &= ~cancel_block_bmp;
                op_mode = 0;/* remove pp disable lkup blocks */
            }
        }
        else
        {
            for (part = 0; part < size; part++)
            {
                for(index=0; index<sub_blk_cnt; index++)
                {
                    block_id = sub_blk[index]+part-block_base;

                    cancel_block_bmp |= 1<<block_id;
                }
            }
            
            block_id = league->acl_priority+block_base;
            SYS_ACL_PART_LOOP(part, size)
            {
                cancel_block_bmp |= p_usw_acl_master[lchip]->league[block_id+part].lkup_level_bitmap;
            }
            
            *lkup_pp_en_bitmap &= ~cancel_block_bmp;
            set_block_bmp = cancel_block_bmp;
            
            op_mode = 0;/* remove pp disable lkup blocks */
        }
        /* if pp league only pp en level blocks, has no need to set stats ram property */

    }

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM,"global lkup lchip:%u, dir:%u, glb_block_bmp:0x%08x, is_add:%d\n", 
                                            lchip, league->dir, p_usw_acl_master[lchip]->global_pp_en_bmp[league->dir], is_add);

    /* set stats ram to global or pp */
    if (MCHIP_F_STATS(lchip)->set_ram_pp_property)
    {
        MCHIP_F_STATS(lchip)->set_ram_pp_property(lchip, league->dir, league->acl_priority, set_block_bmp, op_mode);
    }
    return CTC_E_NONE;
}


#define __BLOCK_PROCESS__

#if 0
/*copy lt key and action(ad and key gen profile) from block to new block*/
STATIC int32
sys_usw_acl_copy_lt_block(uint8 lchip, uint8 block_id, uint8 new_block_id)
{
    uint32 cmd = 0;
    uint8 index = 0;
    ds_t key;
    ds_t mask;
    tbl_entry_t   tcam_key;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    tcam_key.data_entry = (uint32*)&key;
    tcam_key.mask_entry = (uint32*)&mask;

    sys_tmm_acl_copy_presel_ad(lchip, block_id, new_block_id, FALSE);

    for (index = 0; index < 32; index++)
    {
        /*Copy LT KEY*/
        cmd = DRV_IOR(DsLtidSelectTcam_t, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, index + block_id*32, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &tcam_key));

        cmd = DRV_IOW(DsLtidSelectTcam_t, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, index + new_block_id*32, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &tcam_key));
    }

    return CTC_E_NONE;
}

/*clear block part by part*/
STATIC int32
sys_tmm_acl_free_block(uint8 lchip, uint8 block_id)
{
    uint8 part = 0;
    uint8 part_num = 0;
    sys_acl_block_t * pb = NULL;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    pb = &(SYS_ACL_BLOCK(lchip, block_id));

    part_num = pb->fpab.part_num;


    for (part = 0; part < part_num; part++)
    {
        pb = &(SYS_ACL_BLOCK(lchip, block_id + part));
        sal_memset(pb, 0, sizeof(sys_acl_block_t));
    }

    return 0;
}

/*compress block, here block id is the id of first block*/
STATIC int32
sys_tmm_acl_compress_block(uint8 lchip, uint8 block_id)
{
    sys_acl_block_t * pb = NULL;
    uint8 i = 0;
    ctc_fpa_block_t* old_fpab = NULL;
    uint16 start_offset_160 = 0;
    uint16 start_offset_320 = 0;
    uint16 start_offset_640 = 0;
    int32 shift_160 = 0;
    int32 shift_320 = 0;
    int32 shift_640 = 0;
    int32 vary_160 = 0;
    int32 vary_320 = 0;
    int32 vary_640 = 0;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    pb = &(SYS_ACL_BLOCK(lchip, block_id));

    /*not expand block, return*/
    if (NULL == pb->next)
    {
        return 0;
    }

    /*no left to commperss, return*/
    if (pb->fpab.free_count < pb->entry_num)
    {
        return 0;
    }

    old_fpab = &pb->fpab;

    start_offset_160 = 0;
    start_offset_320 = start_offset_160 + (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160]);
    start_offset_640 = start_offset_320 + (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320]);

    vary_160 = (int32)(start_offset_320 - start_offset_160)  - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160];
    vary_320 = (int32)(start_offset_640 - start_offset_320)  - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320];
    vary_640 = (int32)(old_fpab->entry_count - start_offset_640) - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640];

    shift_160 = (int32)start_offset_160 - (int32)old_fpab->start_offset[CTC_FPA_KEY_SIZE_160];
    shift_320 = (int32)start_offset_320 - (int32)old_fpab->start_offset[CTC_FPA_KEY_SIZE_320];
    shift_640 = (int32)start_offset_640 - (int32)old_fpab->start_offset[CTC_FPA_KEY_SIZE_640];

    fpa_usw_decrease(p_usw_acl_master[lchip]->fpa, &pb->fpab, CTC_FPA_KEY_SIZE_160, shift_160, vary_160);
    old_fpab->start_offset[CTC_FPA_KEY_SIZE_160] = start_offset_160;
    old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
    old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] += vary_160;

    fpa_usw_decrease(p_usw_acl_master[lchip]->fpa, &pb->fpab, CTC_FPA_KEY_SIZE_320, shift_320, vary_320);
    old_fpab->start_offset[CTC_FPA_KEY_SIZE_320] = start_offset_320;
    old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += vary_320;
    old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += vary_320;

    fpa_usw_decrease(p_usw_acl_master[lchip]->fpa, &pb->fpab, CTC_FPA_KEY_SIZE_640, shift_640, vary_640);
    old_fpab->start_offset[CTC_FPA_KEY_SIZE_640] = start_offset_640;
    old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] += vary_640;
    old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640] += vary_640;


    /*Get the last pb*/
    while (pb->next)
    {
        pb = pb->next;
    }

    /*Free pb blocks*/
    for (i = 0; i < pb->fpab.free_count / pb->entry_num; i++)
    {
        /*free pb block*/
        sys_tmm_acl_free_block(lchip, pb->block_id);

        old_fpab->entry_count -= pb->entry_num;
        old_fpab->free_count -= pb->entry_num;
        pb = pb->prev;
    }

    return 0;
}
#endif

int32
_sys_tmm_acl_block_set_default(uint8 lchip, sys_acl_block_t *pb, uint8 keep_action_pri)
{
    uint16 start_offset = 0;
    uint16 entry_num = 0;
    uint32 cmd;
    IpeAclGlobalActionPriorityCtl_m act_prio_ctl;
    uint32 act_prio_table[CTC_BOTH_DIRECTION] = {IpeAclGlobalActionPriorityCtl_t, EpeAclGlobalActionPriorityCtl_t};
    uint32 max_block_num = ACL_IGS_BLOCK_MAX_NUM;
    uint8  lkup_level = pb->block_id;
    uint8  dir = CTC_INGRESS;
    uint8 global_pp_en = 0;

    if(pb->block_id >= ACL_IGS_BLOCK_MAX_NUM)
    {
        lkup_level = pb->block_id - ACL_IGS_BLOCK_MAX_NUM;
        max_block_num = ACL_EGS_BLOCK_MAX_NUM;
        dir = CTC_EGRESS;
    }

    entry_num = pb->entry_num;
    pb->valid = 0;
    pb->expand_blk_id = 0;
    pb->next = NULL;
    pb->prev = NULL;
    pb->fpab.same_size = 1;

    start_offset = 0;
    pb->fpab.start_offset[CTC_FPA_KEY_SIZE_160]    = start_offset;
    pb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160] = entry_num / 2;
    pb->fpab.sub_free_count[CTC_FPA_KEY_SIZE_160]  = entry_num / 2;
    pb->fpab.sub_rsv_count[CTC_FPA_KEY_SIZE_160]  = 0;
    start_offset += entry_num / 2;
    pb->fpab.start_offset[CTC_FPA_KEY_SIZE_320]    = start_offset;
    pb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_320] = entry_num / 4;
    pb->fpab.sub_free_count[CTC_FPA_KEY_SIZE_320]  = entry_num / 4;
    pb->fpab.sub_rsv_count[CTC_FPA_KEY_SIZE_320]  = 0;
    start_offset += entry_num / 4;
    pb->fpab.start_offset[CTC_FPA_KEY_SIZE_640]    = start_offset;
    pb->fpab.sub_entry_count[CTC_FPA_KEY_SIZE_640] = entry_num / 4;
    pb->fpab.sub_free_count[CTC_FPA_KEY_SIZE_640]  = entry_num / 4;
    pb->fpab.sub_rsv_count[CTC_FPA_KEY_SIZE_640]  = 0;

    pb->fpab.same_size = 1;
    pb->fpab.part = 0;
    pb->fpab.part_num = 1;
    pb->fpab.next = NULL;
    pb->fpab.vblock = NULL;

    global_pp_en = SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, dir, lkup_level);

    if(!keep_action_pri)
    {
        cmd = DRV_IOR(act_prio_table[dir], DRV_ENTRY_FLAG);
        DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &act_prio_ctl);
        SetIpeAclGlobalActionPriorityCtl(V, gLookup_0_actionPriority_f + lkup_level, &act_prio_ctl, max_block_num - lkup_level);
        cmd = DRV_IOW(act_prio_table[dir], DRV_ENTRY_FLAG);
        DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &act_prio_ctl);
    }
    return 0;
}

sys_acl_flex_key_t*
sys_tmm_acl_get_flex_key(uint8 lchip, uint8 dir, uint8 key_type)
{
    sys_acl_flex_key_t *fk = NULL;
    fk = ctc_vector_get(SYS_ACL_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(dir, key_type));
    if ((NULL == fk) && SYS_ACL_KEY_IS_RSVD(key_type))
    {
        fk = ctc_vector_get(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(dir, key_type));
    }
    return fk;
}

int32
sys_tmm_acl_expand_link_block(uint8 lchip,
                              sys_acl_block_t* pb,
                              sys_acl_flex_key_t* fk,
                              uint8 part_num)
{
    uint32 cmd = 0;
    sys_acl_league_glb_config_t glb_config;
    ctc_fpa_block_t *p_fpab = NULL;
    sys_acl_league_t* p_league1 = NULL;
    IpeAclKeySizeCtl_m key_size_ctl;
    uint32 key_size_table[CTC_BOTH_DIRECTION] = {IpeAclKeySizeCtl_t, EpeAclKeySizeCtl_t};
    uint32 act_prio_table[CTC_BOTH_DIRECTION] = {IpeAclGlobalActionPriorityCtl_t, EpeAclGlobalActionPriorityCtl_t};
    uint8 part = 0;
    uint8 temp_data = 0;
    uint8 old_part_num = 0;
    uint8 block_id = pb->block_id;
    uint8 dir = CTC_INGRESS;
    uint8 acl_prio = block_id;
    uint8 max_block_num = ACL_IGS_BLOCK_MAX_NUM;
    IpeAclGlobalActionPriorityCtl_m act_prio_ctl;
    uint8 global_pp_en = 0;

    SYS_ACL_DBG_FUNC();

    if (block_id>=ACL_IGS_BLOCK_MAX_NUM)
    {
        dir = CTC_EGRESS;
        acl_prio = block_id-ACL_IGS_BLOCK_MAX_NUM;
        max_block_num = ACL_EGS_BLOCK_MAX_NUM;
    }
    pb = &(SYS_ACL_BLOCK(lchip, block_id));
    p_league1 = &p_usw_acl_master[lchip]->league[block_id];
    old_part_num = pb->fpab.part_num;

    temp_data = SYS_ACL_GET_LEAGUE_SIZE(block_id);
    if ((temp_data < part_num) && !SYS_ACL_BLOCK_IS_UNMERGED(lchip, block_id))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "League size(%u) less than part num(%u)!\n", temp_data, part_num);
        CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
    }

    if (acl_prio + part_num > max_block_num)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Invalid block used!\n");
        CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
    }

    for (part = 0; part < part_num; part++)
    {
        if (pb[part].valid && pb[part].expand_blk_id != block_id)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Invalid action priority used!, part:%d,expand_blk_id:%d,block_id:%d\n", part, pb[part].expand_blk_id, block_id);
            CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
        }
    }

    if ((p_league1->merged_to != block_id) || (p_league1->hdr_block_id != block_id))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Must be header block!\n");
        CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
    }
    for (part=0; part<part_num; part++)
    {
        if ((p_league1[part].hdr_block_id != block_id) && !SYS_ACL_BLOCK_IS_UNMERGED(lchip, block_id+part))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "League size less than part num!\n");
            CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
        }
    }

    /*update ltid ad and gen profile*/
    if (fk)
    {
        sys_tmm_acl_entry_add_presel(lchip, block_id, fk);
    }
    if (1 == pb[0].valid && part_num <= pb[0].fpab.part_num)
    {
        return CTC_E_NONE;
    }

    global_pp_en = SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, dir, acl_prio);
    
    sys_tmm_acl_presel_with_block(lchip, block_id, part_num, global_pp_en);
    p_fpab = pb->fpab.vblock;

    if (part_num > 1 && p_fpab == NULL && !SYS_USW_ACL_UPA_EN(lchip, CTC_INGRESS))
    {
        p_fpab  = mem_malloc(MEM_ACL_MODULE, sizeof(ctc_fpa_block_t));
        if (NULL == p_fpab)
        {
            CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
        }

        sal_memset(p_fpab, 0, sizeof(ctc_fpa_block_t));
        p_fpab->same_size = 1;
        p_fpab->part_num = 1;
        p_fpab->entry_count = p_fpab->free_count = pb->fpab.entry_count*3;

        MALLOC_ZERO(MEM_ACL_MODULE, p_fpab->entries, p_fpab->entry_count*sizeof(sys_acl_entry_t*));
        if (NULL == p_fpab->entries)
        {
            mem_free(p_fpab);
            CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
        }

        pb->fpab.vblock = p_fpab;
    }

    /*update DB*/
    _sys_tmm_acl_init_vblock(lchip, block_id, part_num, FALSE);

    sal_memset(&glb_config, 0, sizeof(sys_acl_league_glb_config_t));
    glb_config.block_id = block_id;
    glb_config.op_type = SYS_ACL_LEAGUE_CONFIG_EXPAND;
    glb_config.old_part_num = old_part_num;
    glb_config.global_pp_en = global_pp_en;    
    sys_tmm_acl_set_league_global_config(lchip, &glb_config);

    /* config lkup key size for league */
    if (pb[0].block_status == SYS_ACL_BLOCK_STATUS_UNMERGE)
    {
        cmd = DRV_IOR(key_size_table[dir], DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &key_size_ctl));
        SetIpeAclKeySizeCtl(V, gAclLookup_0_keySizeType_f + acl_prio, &key_size_ctl, part_num);
        cmd = DRV_IOW(key_size_table[dir], DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &key_size_ctl));
    }

    cmd = DRV_IOR(act_prio_table[dir], DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &act_prio_ctl);
    temp_data = GetIpeAclGlobalActionPriorityCtl(V, gLookup_0_actionPriority_f + acl_prio, &act_prio_ctl);
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_BLOCK, 1);
    for (part=1; part<part_num; part++)
    {
        SetIpeAclGlobalActionPriorityCtl(V, gLookup_0_actionPriority_f + acl_prio + part, &act_prio_ctl, temp_data);
    }
    cmd = DRV_IOW(act_prio_table[dir], DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &act_prio_ctl);
    return 0;
}




int32
sys_tmm_acl_compress_link_block(uint8 lchip,
                                sys_acl_block_t *pb,
                                sys_acl_flex_key_t* fk)
{
    uint8 old_part_num = 0;
    uint8 part_num = 0;
    uint8 do_compress = 0;
    uint8 acl_prio = SYS_ACL_BLOCK_PRIO(pb->block_id);
    uint8 dir = SYS_ACL_BLOCK_DIR(pb->block_id);
    ctc_fpa_block_t *p_fpab = NULL;
    uint32 cmd = 0;
    //uint32 field_val = 0;
    //ProgramAclLkpSelectTcamSlice_m tcam_bmp;
    IpeAclKeySizeCtl_m key_size_ctl;
    sys_acl_league_glb_config_t glb_config;
    uint32 key_size_table[CTC_BOTH_DIRECTION] = {IpeAclKeySizeCtl_t, EpeAclKeySizeCtl_t};
    uint8 global_pp_en = 0;

    if (fk && !fk->entry_count[acl_prio])
    {
        sys_tmm_acl_entry_remove_presel(lchip, pb->block_id, fk);
    }

    if (p_usw_acl_master[lchip]->league[pb->block_id].size != 0 && fk)
    {
        return CTC_E_NONE;
    }

    //step = ProgramAclLkpSelectTcamSlice_gAclLkpLevel_1_selectTcamSliceBitmap_f - ProgramAclLkpSelectTcamSlice_gAclLkpLevel_0_selectTcamSliceBitmap_f;
    //cmd = DRV_IOR(ProgramAclLkpSelectTcamSlice_t, DRV_ENTRY_FLAG);
    //CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &tcam_bmp));
    //field_val = GetProgramAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f + step*acl_prio, &tcam_bmp);

    old_part_num = pb[0].fpab.part_num;
    p_fpab = pb->fpab.vblock;

    global_pp_en = SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, dir, acl_prio);

    if (old_part_num == 3 && BLOCK_EMPTY(pb, 2))  /*remove part 2*/
    {
        sys_tmm_acl_presel_with_block(lchip, pb->block_id, 2, global_pp_en);
        if (p_fpab)
        {
            p_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] = pb[0].fpab.sub_entry_count[CTC_FPA_KEY_SIZE_160]*2;
            p_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160]  =  pb[0].fpab.sub_free_count[CTC_FPA_KEY_SIZE_160] + pb[1].fpab.sub_free_count[CTC_FPA_KEY_SIZE_160];
        }

        pb[0].fpab.part_num = 2;
        pb[1].fpab.part_num = 2;
        pb[1].fpab.next = NULL;
        if (pb[2].block_status==SYS_ACL_BLOCK_STATUS_UNMERGE )
        {
            _sys_tmm_acl_block_set_default(lchip,  &pb[2], 0);
        }
        else
        {
            pb[2].fpab.part_num = 1;
            pb[2].fpab.part = 0;
            pb[2].valid = 0;
        }

        part_num = 2;
        do_compress = 1;

        //CTC_BIT_UNSET(field_val, acl_prio+2);
    }

    if (((old_part_num == 2) && BLOCK_EMPTY(pb, 1)) ||
        ((old_part_num == 3) && BLOCK_EMPTY(pb, 1) && BLOCK_EMPTY(pb, 2)))  /*remove part 1*/
    {
        sys_tmm_acl_presel_with_block(lchip, pb->block_id, 1, global_pp_en);
        pb[0].fpab.next = NULL;
        pb[0].fpab.part_num = 1;
        if (pb[1].block_status==SYS_ACL_BLOCK_STATUS_UNMERGE)
        {
            _sys_tmm_acl_block_set_default(lchip,  &pb[1], 0);
        }
        else
        {
            pb[1].fpab.part_num = 1;
            pb[1].fpab.part = 0;
            pb[1].valid = 0;
        }
        if (pb->fpab.vblock)
        {
            mem_free(pb->fpab.vblock->entries);
            mem_free(pb->fpab.vblock);
        }
        part_num = 1;
        do_compress = 1;

        //CTC_BIT_UNSET(field_val, acl_prio+1);
    }

    if (((old_part_num == 1) && BLOCK_EMPTY(pb, 0)) ||
        ((old_part_num == 2) && BLOCK_EMPTY(pb, 0) && BLOCK_EMPTY(pb, 1)) ||
        ((old_part_num == 3) && BLOCK_EMPTY(pb, 0) && BLOCK_EMPTY(pb, 1) && BLOCK_EMPTY(pb, 2)))  /*remove part 0*/
    {
        if (pb[0].block_status==SYS_ACL_BLOCK_STATUS_UNMERGE)
        {
            _sys_tmm_acl_block_set_default(lchip,  &pb[0], 1);
        }
        else
        {
            pb[0].fpab.part_num = 1;
            pb[0].fpab.part = 0;
            pb[0].valid = 0;
        }
        part_num = 0;
        do_compress = 1;
        //CTC_BIT_UNSET(field_val, acl_prio);
    }

    if (pb[0].block_status == SYS_ACL_BLOCK_STATUS_UNMERGE && do_compress)
    {
        sal_memset(&glb_config, 0, sizeof(sys_acl_league_glb_config_t));
        glb_config.block_id = pb->block_id;
        glb_config.op_type = SYS_ACL_LEAGUE_CONFIG_COMPRESS;
        glb_config.old_part_num = old_part_num;
        glb_config.global_pp_en = global_pp_en;
        sys_tmm_acl_set_league_global_config(lchip, &glb_config);

        cmd = DRV_IOR(key_size_table[dir], DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &key_size_ctl));
        SetIpeAclKeySizeCtl(V, gAclLookup_0_keySizeType_f + acl_prio, &key_size_ctl, part_num);
        cmd = DRV_IOW(key_size_table[dir], DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &key_size_ctl));
    }
    return CTC_E_NONE;
}



#define ___LT_TCAM____


#define SYS_ACL_FOUND_CID(type, cid, found) found = ((type) == (cid)? TRUE: FALSE)
int32
sys_tmm_acl_get_presel_cid(uint8 lchip, uint32 type, uint8 dir, uint8* index)
{
    int8 cid[4] = { -1, -1, -1, -1};
    uint8 loop = 0;
    uint8 found = FALSE;
    uint32 cmd = 0;
    ProgramAclLtidSelectConfig_m config;
    uint32 config_table[CTC_BOTH_DIRECTION] = {ProgramAclLtidSelectConfig_t, EgrProgramAclLtidSelectConfig_t};

    sal_memset(&config, 0, sizeof(config));
    cmd = DRV_IOR(config_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &config));

    for (loop = 0; loop < 4; loop++)
    {
        cid[loop] = GetProgramAclLtidSelectConfig(V, categoryId0Sel_f + loop,  &config);
        switch(cid[loop])
        {
        case 0:
            SYS_ACL_FOUND_CID(type, CTC_FIELD_KEY_PORT_CID, found);
            break;
        case 1:
            SYS_ACL_FOUND_CID(type, CTC_FIELD_KEY_VLAN_CID, found);
            break;
        case 2:
            SYS_ACL_FOUND_CID(type, CTC_FIELD_KEY_L3IF_CID, found);
            break;
        case 3:
            SYS_ACL_FOUND_CID(type, (dir==CTC_INGRESS)?(CTC_FIELD_KEY_SCL_CID):(CTC_FIELD_KEY_NEXTHOP_CID), found);
            break;
        case 4:
            SYS_ACL_FOUND_CID(type, CTC_FIELD_KEY_TUNNEL_CID, found);   /*egress, CTC_FIELD_KEY_I2E_CID, no-use*/
            break;
        case 5:
            SYS_ACL_FOUND_CID(type, (dir==CTC_INGRESS)?(CTC_FIELD_KEY_FLOW_CID):(CTC_FIELD_KEY_LDP_CID), found);
            break;
        case 6:
            SYS_ACL_FOUND_CID(type, CTC_FIELD_KEY_FID_CID, found);
            break;
        default:
            break;
        }

        if (found)
        {
            break;
        }
    }

    if (!found)
    {
        CTC_ERROR_RETURN(CTC_E_INVALID_PARAM);
    }
    *index = loop;
    return CTC_E_NONE;
}



int32
sys_tmm_acl_build_presel_field(uint8 lchip,
                               uint8 dir,
                               tbl_entry_t buffer,
                               ctc_field_key_t* key_field,
                               uint8 is_add)
{
    uint8  index = 0;
    uint8 pkt_fwd_type = 0;
    uint32 *p_key = NULL;
    uint32 *p_mask = NULL;
    uint32 data = 0;
    uint32 mask = 0;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    p_key = buffer.data_entry;
    p_mask = buffer.mask_entry;
    data = is_add ? key_field->data : 0;
    mask = is_add ? key_field->mask : 0;

    switch(key_field->type)
    {
    case CTC_FIELD_KEY_L3_TYPE:
        CTC_MAX_VALUE_CHECK(data, MAX_CTC_PARSER_L3_TYPE);
        if(data == CTC_PARSER_L3_TYPE_IP)
        {
            data = CTC_PARSER_L3_TYPE_IPV4;
			mask = 0xe;
        }
        SetDsLtidSelectTcam(V, layer3Type_f, p_key, data);
        SetDsLtidSelectTcam(V, layer3Type_f, p_mask, mask);
        break;
    case CTC_FIELD_KEY_L3_EXT_TYPE:
        CTC_MAX_VALUE_CHECK(data, CTC_PARSER_L3_EXT_TYPE_NUM);
        SetDsLtidSelectTcam(V, layer3ExtType_f, p_key, data);
        SetDsLtidSelectTcam(V, layer3ExtType_f, p_mask, mask);
        break;

    case CTC_FIELD_KEY_L4_TYPE:
        CTC_MAX_VALUE_CHECK(data, MAX_CTC_PARSER_L4_TYPE);
        SetDsLtidSelectTcam(V, layer4Type_f, p_key, sys_usw_l4_type_map(lchip, data, TRUE));
        SetDsLtidSelectTcam(V, layer4Type_f, p_mask, mask);

        SetDsLtidSelectTcam(V, layer4Valid_f, p_key, is_add?1:0);
        SetDsLtidSelectTcam(V, layer4Valid_f, p_mask, is_add?1:0);
        break;

    case CTC_FIELD_KEY_L4_USER_TYPE:
        CTC_MAX_VALUE_CHECK(data, 0xF);
        SetDsLtidSelectTcam(V, layer4UserType_f, p_key, is_add? sys_usw_l4_user_type_map(lchip, key_field->data, 1): 0);
        SetDsLtidSelectTcam(V, layer4UserType_f, p_mask, mask);

        SetDsLtidSelectTcam(V, layer4UserTypeValid_f, p_key, is_add?1:0);
        SetDsLtidSelectTcam(V, layer4UserTypeValid_f, p_mask, is_add?1:0);
        break;

    case CTC_FIELD_KEY_DECAP:
        SetDsLtidSelectTcam(V, isDecap_f, p_key, (data != 0)? 1: 0);
        SetDsLtidSelectTcam(V, isDecap_f, p_mask, mask & 0x1);
        break;

    case CTC_FIELD_KEY_ROUTED_PKT:
        SetDsLtidSelectTcam(V, isRouterMac_f, p_key, (data != 0)? 1: 0);
        SetDsLtidSelectTcam(V, isRouterMac_f, p_mask, mask & 0x1);
        break;

    case CTC_FIELD_KEY_IP_HDR_ERROR:
        CTC_MAX_VALUE_CHECK(data, 1);
        SetDsLtidSelectTcam(V, ipHeaderError_f, p_key, data);
        SetDsLtidSelectTcam(V, ipHeaderError_f, p_mask, mask);
        break;

    case CTC_FIELD_KEY_IP_OPTIONS:
        CTC_MAX_VALUE_CHECK(data, 1);
        SetDsLtidSelectTcam(V, ipOption_f, p_key, data);
        SetDsLtidSelectTcam(V, ipOption_f, p_mask, mask);
        break;

    case CTC_FIELD_KEY_DISCARD:
        SetDsLtidSelectTcam(V, discard_f, p_key, (data != 0)? 1: 0);
        SetDsLtidSelectTcam(V, discard_f, p_mask, mask & 0x1);
        break;

    case CTC_FIELD_KEY_MIRROR_PKT:
        SetDsLtidSelectTcam(V, isSpanPkt_f, p_key, (data != 0)? 1: 0);
        SetDsLtidSelectTcam(V, isSpanPkt_f, p_mask, mask & 0x1);
        break;

    case CTC_FIELD_KEY_PKT_FWD_TYPE:
        CTC_MAX_VALUE_CHECK(data, 0xF);
        sys_usw_acl_map_fwd_type((uint8)data, &pkt_fwd_type);
        SetDsLtidSelectTcam(V, pktForwardingType_f, p_key, pkt_fwd_type);
        SetDsLtidSelectTcam(V, pktForwardingType_f, p_mask, mask);
        break;

    case CTC_FIELD_KEY_PORT_CID:
    case CTC_FIELD_KEY_VLAN_CID:
    case CTC_FIELD_KEY_L3IF_CID:
    case CTC_FIELD_KEY_TUNNEL_CID:
    case CTC_FIELD_KEY_SCL_CID:
    case CTC_FIELD_KEY_FLOW_CID:
    case CTC_FIELD_KEY_FID_CID:
    case CTC_FIELD_KEY_NEXTHOP_CID:
    case CTC_FIELD_KEY_LDP_CID:
        CTC_MAX_VALUE_CHECK(data, MCHIP_CAP(SYS_CAP_ACL_MAX_CID_VALUE));
        CTC_ERROR_RETURN(sys_tmm_acl_get_presel_cid(lchip, key_field->type, dir, &index));
        SetDsLtidSelectTcam(V, categoryId0_f + index, p_key, data);
        SetDsLtidSelectTcam(V, categoryId0_f + index, p_mask, mask);
        break;

    case CTC_FIELD_KEY_PRESEL_ENTRY_VALID:
        break;/*process after all fields added*/
    default:
        return CTC_E_INVALID_PARAM;
    }

    return CTC_E_NONE;
}

/*map ctc key field to sys field field*/
int32
_sys_tmm_acl_map_xdata_bmp(uint8 lchip, ctc_acl_flex_key_t* ctc_fk, sys_acl_flex_key_t* fk)
{
    uint8 step[3] = {4, 2, 1};
    uint8 sel_max[3] = {2, 4, 4};
    int8* f_sel=NULL;
    uint8 i = 0;
    uint8 sel_num[3] = {0};
    uint8 loop = 0;
    uint8 is_stk = ctc_fk->key_type == CTC_ACL_KEY_STK_HDR;
    uint8 max_byte_num = 0;
    uint8 ctl_sel_star[3] = { SYS_ACL_KEXT_XDATA_SEL_DW0, 
                              SYS_ACL_KEXT_XDATA_SEL_W0 ,
                              SYS_ACL_KEXT_XDATA_SEL_B0 };
    uint32 sel_field[3] =  { SYS_ACL_FIELD_XDATA_DW0,
                            SYS_ACL_FIELD_XDATA_W0 ,
                            SYS_ACL_FIELD_XDATA_B0 };
    uint16 step_bmp = 0;
    uint16 xdata_bmp = 0;

    CTC_PTR_VALID_CHECK(ctc_fk->xdata.fields);
    if(is_stk)
    {
        max_byte_num = 8;
        sel_max[1] = 2;
        sel_field[0] = SYS_ACL_FIELD_STK_XDATA_DW0;
        sel_field[1] = SYS_ACL_FIELD_STK_XDATA_W0;
        sel_field[2] = SYS_ACL_FIELD_STK_XDATA_B0;
    }
    else
    {
        max_byte_num = 16;
        f_sel = fk->xdata_sel;
        sal_memset(f_sel, -1, SYS_ACL_KEXT_XDATA_SEL_MAX);
    }

    CTC_BMP_UNSET(fk->kset.w, CTC_FIELD_KEY_XDATA);
    for(loop = 0; loop < ctc_fk->xdata.num; loop++)
    {
        uint8 byte_offset = 0;
        ctc_xdata_field_t* x_field = &ctc_fk->xdata.fields[loop];
        switch(x_field->width)
        {
            case CTC_XDATA_WIDTH_8:
                step_bmp = 1;
                break;
            case CTC_XDATA_WIDTH_16:
                step_bmp = 0x3;
                break;
            case CTC_XDATA_WIDTH_32:
                step_bmp = 0xF;
                break;
            default:
                return CTC_E_INVALID_CONFIG;
        }
        byte_offset = (x_field->offset/CTC_XDATA_WIDTH_8);
        if(x_field->offset % x_field->width || byte_offset >= max_byte_num)
        {
            return CTC_E_INVALID_CONFIG;
        }
        xdata_bmp |= step_bmp << byte_offset;
    }
    
    for(i = 0; i<3; i++)
    {
        step_bmp = (1<< step[i])-1;
        for(loop = 0; loop < max_byte_num; loop += step[i])
        {
            if((xdata_bmp & step_bmp) == step_bmp && sel_num[i] < sel_max[i])
            {
                if(is_stk)
                {   /* stacking xdata 16bit only 0-31, 8bit only 32-63 */
                    if((step[i] == 2 && loop > 3) || (step[i] == 1 && loop < 4))
                    {
                        step_bmp = step_bmp << step[i];
                        continue;
                    }
                    CTC_BMP_SET(fk->kset.w, sel_field[i] + loop/step[i] - (step[i] == 1 ? 4 : 0 ));
                }
                else
                {
                    CTC_BMP_SET(fk->kset.w, sel_field[i] + sel_num[i]);
                    f_sel[ctl_sel_star[i] + sel_num[i]] = loop/step[i];
                }
                xdata_bmp &= (~step_bmp);
                sel_num[i]++;
            }
            step_bmp = step_bmp << step[i];
        }
    }
    if(xdata_bmp)
    {
        i = 2;
        do  /* first is 2 */
        {
            step_bmp = (1<< step[i])-1;
            for(loop = 0; loop < max_byte_num; loop += step[i])
            {
                if((xdata_bmp & step_bmp) && sel_num[i] <sel_max[i])
                {
                    if(is_stk)
                    {
                        if((step[i] == 2 && loop > 3) || (step[i] == 1 && loop < 4))
                        {
                            step_bmp = step_bmp << step[i];
                            continue;
                        }
                        CTC_BMP_SET(fk->kset.w, sel_field[i] + loop/step[i] - (step[i] == 1 ? 4 : 0 ));
                    }
                    else
                    {
                        CTC_BMP_SET(fk->kset.w, sel_field[i] + sel_num[i]);
                        f_sel[ctl_sel_star[i] + sel_num[i]] = loop/step[i];
                    }
                    xdata_bmp &= (~step_bmp);
                    sel_num[i]++;
                }
                step_bmp = step_bmp << step[i];
            }
        }while(i--);
    }
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_flex_key_update_kset(uint8 lchip, sys_acl_flex_key_t* fk, ctc_acl_flex_key_t* acl_flex_key)
{
    uint8 loop = 0;
    sys_acl_kset_t* kset = &fk->kset;
    uint8 vxlan_en  = CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_VN_ID) ||
                      CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_VXLAN_FLAGS);
    uint8 l4port_en = CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_L4_DST_PORT) ||
                      CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_L4_SRC_PORT);
    
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    if(1 == p_usw_acl_master[SYS_PP_BASE(lchip)]->ifa_ver)
    {
        if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_INT_TYPE))
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_INT_TYPE);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_INT_TYPE);
        }
        if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_INT_VER))
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_INT_VER);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_INT_VER);
        }
        if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_INT_HOP_ML))
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_INT_HOP_ML);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_INT_HOP_ML);
        }
        if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_INT_REMAIN_HOP_CNT))
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_INT_REMAIN_HOP_CNT);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_INT_REMAIN_HOP_CNT);
        }
        if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_INT_INSTRUCTION))
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_INT_INSTRUCTION);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_INT_INSTRUCTION);
        }
    }
    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_VLAN_XLATE_HIT))
    {
        /*SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_VLAN_XLATE_HIT);*/
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_VLAN_XLATE0_HIT);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_VLAN_XLATE1_HIT);
    }
#ifdef nsh
    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_NSH_CONTEXT_HEADER))
    {
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_NSH_CONTEXT_HEADER);
        if (CTC_IS_BIT_SET(acl_flex_key->nsh_ch_bmp, 0))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_NSH_CH_CHUNK0);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->nsh_ch_bmp, 1))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_NSH_CH_CHUNK1);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->nsh_ch_bmp, 2))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_NSH_CH_CHUNK2);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->nsh_ch_bmp, 3))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_NSH_CH_CHUNK3);
        }
    }
#endif

    if ((0 != vxlan_en) && (0 != l4port_en) && acl_flex_key->size > CTC_FPA_KEY_SIZE_160)
    {
        if CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_VN_ID)
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_VN_ID);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_VN_ID_SHARE);
        }
        if CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_VXLAN_FLAGS)
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_VXLAN_FLAGS);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_VXLAN_FLAGS_SHARE);
        }
        if CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_L4_DST_PORT)
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_L4_DST_PORT);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_L4_DP_SHARE);
        }
        if CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_L4_SRC_PORT)
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_L4_SRC_PORT);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_L4_SP_SHARE);
        }
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_IS_Y1731_OAM))
    {
        SYS_ACL_PKSET_ADD(kset, CTC_FIELD_KEY_L4_TYPE);
        SYS_ACL_PKSET_ADD(kset, CTC_FIELD_KEY_L4_USER_TYPE);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_UDF_AD_ID) || CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_UDF))
    {
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_UDF2_PROF_ID);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_UDF2_VALID_BITMAP);
        for (loop=0; loop<CTC_ACL_UDF_FIELD_NUM; loop++)
        {
            if (CTC_IS_BIT_SET(acl_flex_key->udf_bmp, loop))
            {
                SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_UDF2_CHUNK0+loop);
            }
        }
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_UDF_AD_ID);
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_UDF);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_SUDF_AD_ID) || CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_SUDF))
    {
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_UDF1_PROF_ID);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_UDF1_VALID_BITMAP);
        for (loop=0; loop<CTC_ACL_UDF_FIELD_NUM; loop++)
        {
            if (CTC_IS_BIT_SET(acl_flex_key->sudf_bmp, loop))
            {
                SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_UDF1_CHUNK0+loop);
            }
        }
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_SUDF_AD_ID);
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_SUDF);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_DST_NHID))
    {
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_DST_NHID);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_DESTMAP);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_NH_OFFSET);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_DEST_APS);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_DEST_ECMP);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_NH_EXT);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_DST_GPORT))
    {
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_DST_GPORT);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_DESTMAP);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_DEST_APS);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_DEST_ECMP);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_CPU_REASON_ID))
    {
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_CPU_REASON_ID);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_EXCEPTION_GID);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_EXCEPTION_EXCP_EN);
        if(CTC_INGRESS == acl_flex_key->dir)
        {   /* not support for egress */
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_EXCEPTION_FATAL_EN);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_EXCEPTION_FATAL_EXCP_ID);
        }
    }

    if(CTC_EGRESS == acl_flex_key->dir)
    {
        if(CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_LOGIC_SRC_PORT))
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_LOGIC_SRC_PORT);
            SYS_ACL_PKSET_ADD(kset, CTC_FIELD_KEY_FID_CID);
        }
        if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_LOGIC_DST_PORT))
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_LOGIC_DST_PORT);
            SYS_ACL_PKSET_ADD(kset, CTC_FIELD_KEY_LOGIC_SRC_PORT);
        }
        if(CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_NPM_IM_FLOW_ID))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_XD_IM_FLOW_ID);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_XD_IM_L_FLAG);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_XD_IM_D_FLAG);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_XD_IM_SRC_D_FLAG);
            fk->xdata_sel[1] = 1;
        }
    }

    if(DRV_FROM_AT(lchip) && CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_NPM_IM_FLOW_ID))
    {
        SYS_ACL_PKSET_ADD(kset, CTC_FIELD_KEY_NPM_IM_FLOW_ID);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_IM_FLOW_ID);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_IM_L_FLAG);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_IM_D_FLAG);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_STK_HDR_EXIST) && CTC_INGRESS == acl_flex_key->dir && DRV_FROM_AT(lchip))
    {
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_STK_HDR_BMP);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_PORT))
    {
        if(!CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_PORT_BITMAP) && acl_flex_key->port_bitmap_width)
        {
            if(acl_flex_key->port_bitmap_width >= CTC_ACL_PORT_BITMAP_WIDTH_32)
            {
                SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_PORT_BITMAP_MODE0 + acl_flex_key->port_bitmap_width-CTC_ACL_PORT_BITMAP_WIDTH_32);
            }
            else
            {
                SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_PBM);
            }
            if(CTC_FLAG_ISSET(acl_flex_key->flag, CTC_ACL_FLEX_FLAG_PBM_NO_GPORT))
            {
                SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_PORT);
            }
        }
        else
        {
            SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_PORT);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_GPORT_SHARE);
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_PBM);
        }
    }
    
    if(CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_LOGIC_SRC_PORT) && CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_PORT))
    {
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_LOGIC_SRC_PORT);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_LOGIC_PORT_SHARE);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_PORT_BITMAP) && acl_flex_key->port_bitmap_width >= CTC_ACL_PORT_BITMAP_WIDTH_32)
    {
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_PORT_BITMAP_MODE0 + acl_flex_key->port_bitmap_width-CTC_ACL_PORT_BITMAP_WIDTH_32);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_IPV6_DA))
    {
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_IPV6_DA);
        if (CTC_IS_BIT_SET(acl_flex_key->ipv6_da_bmp, 0))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_DA_CHUNK0);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->ipv6_da_bmp, 1))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_DA_CHUNK1);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->ipv6_da_bmp, 2))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_DA_CHUNK2);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->ipv6_da_bmp, 3))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_DA_CHUNK3);
        }
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_IPV6_SA))
    {
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_IPV6_SA);
        if (CTC_IS_BIT_SET(acl_flex_key->ipv6_sa_bmp, 0))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_SA_CHUNK0);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->ipv6_sa_bmp, 1))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_SA_CHUNK1);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->ipv6_sa_bmp, 2))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_SA_CHUNK2);
        }
        if (CTC_IS_BIT_SET(acl_flex_key->ipv6_sa_bmp, 3))
        {
            SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IPV6_SA_CHUNK3);
        }
    }
    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_IP_FRAG) && !(CTC_ACL_KEY_IPV4_EXT == fk->key_type && CTC_EGRESS == acl_flex_key->dir))
    {
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_IS_FRAG);
    }

    if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_ETHER_TYPE) && !acl_flex_key->compress_ether_type)
    {
        SYS_ACL_PKSET_REMOVE(kset, CTC_FIELD_KEY_ETHER_TYPE);
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_FULL_ETHER_TYPE);
    }

    if ((DRV_IS_TMM(lchip) || DRV_IS_TMG(lchip)) && (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_SRV6_SRH_FLAG) ||
        CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_SRV6_SRH_NH) ||
        CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_SRV6_SRH_RT) ||
        CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_SRV6_SRH_SL) ||
        CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_SRV6_SRH_LE) ||
        CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_SRV6_SRH_TAG)))
    {
        SYS_ACL_PKSET_ADD(kset, SYS_ACL_FIELD_SRH_COMMON_HDR);
    }
    if(CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_XDATA))
    {
        CTC_ERROR_RETURN(_sys_tmm_acl_map_xdata_bmp(lchip, acl_flex_key, fk));
    }
    return CTC_E_NONE;
}

STATIC void
_sys_tmm_acl_flex_key_unmap_kset(uint8 lchip, sys_acl_kset_t* kset, ctc_acl_flex_key_t* flex_key)
{
    uint32 loop = 0;
    ctc_acl_kset_t* p_kset = &flex_key->kset;

    sal_memcpy(flex_key->kset.w, kset->w, sizeof(flex_key->kset.w));
    if(1 == p_usw_acl_master[SYS_PP_BASE(lchip)]->ifa_ver)
    {
        if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_INT_TYPE))
        {
            SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_INT_TYPE);
        }
        if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_INT_VER))
        {
            SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_INT_VER);
        }
        if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_INT_HOP_ML))
        {
            SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_INT_HOP_ML);
        }
        if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_INT_REMAIN_HOP_CNT))
        {
            SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_INT_REMAIN_HOP_CNT);
        }
        if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_INT_INSTRUCTION))
        {
            SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_INT_INSTRUCTION);
        }
    }

    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_L4_DP_SHARE))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_L4_DST_PORT);
    }
    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_L4_SP_SHARE))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_L4_SRC_PORT);
    }
    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_VN_ID_SHARE))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_VN_ID);
    }
    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_VXLAN_FLAGS_SHARE))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_VXLAN_FLAGS);
    }
    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_GPORT_SHARE) || CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_LOGIC_PORT_SHARE))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_PORT);
    }
    if ((CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_LOGIC_SRC_PORT) || CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_LOGIC_PORT_SHARE))
         && CTC_EGRESS == flex_key->dir)
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_LOGIC_DST_PORT);
        SYS_ACL_PKSET_REMOVE(p_kset, CTC_FIELD_KEY_LOGIC_SRC_PORT);
    }
    if(CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_FID_CID) && CTC_EGRESS == flex_key->dir)
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_LOGIC_SRC_PORT);
        SYS_ACL_PKSET_REMOVE(p_kset, CTC_FIELD_KEY_FID_CID);
    }
    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_UDF1_VALID_BITMAP))
    {
        for (loop=SYS_ACL_FIELD_UDF1_CHUNK0; loop<SYS_ACL_FIELD_UDF1_CHUNK7+1; loop++)
        {
            if (!CTC_ACL_KSET_ISSET(*kset, loop))
            {
                continue;
            }
            CTC_BIT_SET(flex_key->sudf_bmp, loop-SYS_ACL_FIELD_UDF1_CHUNK0);
        }
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_SUDF);
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_SUDF_AD_ID);
    }

    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_UDF2_VALID_BITMAP))
    {
        for (loop=SYS_ACL_FIELD_UDF2_CHUNK0; loop<SYS_ACL_FIELD_UDF2_CHUNK7+1; loop++)
        {
            if (!CTC_ACL_KSET_ISSET(*kset, loop))
            {
                continue;
            }
            CTC_BIT_SET(flex_key->udf_bmp, loop-SYS_ACL_FIELD_UDF2_CHUNK0);
        }
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_UDF);
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_UDF_AD_ID);
    }

    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_NH_OFFSET))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_DST_NHID);
    }
    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_DESTMAP))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_DST_GPORT);
    }

    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_EXCEPTION_GID))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_CPU_REASON_ID);
    }
    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_IS_FRAG))
    {
        SYS_ACL_PKSET_ADD(kset, CTC_FIELD_KEY_IP_FRAG);
    }

    loop = SYS_ACL_FIELD_PBM;
    do
    {
        if (!CTC_ACL_KSET_ISSET(*kset, loop))
        {
            continue;
        }
        flex_key->port_bitmap_width = loop-SYS_ACL_FIELD_PBM + CTC_ACL_PORT_BITMAP_WIDTH_16_2;
        if(!CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_PORT))
        {
            flex_key->flag |= CTC_ACL_FLEX_FLAG_PBM_NO_GPORT;
        }
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_PORT);
        break;
    }while( (++loop) < (SYS_ACL_FIELD_PORT_BITMAP_MODE3+1) );


    loop = SYS_ACL_FIELD_IPV6_DA_CHUNK0;
    do
    {
        if (!CTC_ACL_KSET_ISSET(*kset, loop))
        {
            continue;
        }
        CTC_BIT_SET(flex_key->ipv6_da_bmp, loop-SYS_ACL_FIELD_IPV6_DA_CHUNK0);
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_IPV6_DA);
    }while( (++loop) < (SYS_ACL_FIELD_IPV6_DA_CHUNK3+1) );

    loop = SYS_ACL_FIELD_IPV6_SA_CHUNK0;
    do
    {
        if (!CTC_ACL_KSET_ISSET(*kset, loop))
        {
            continue;
        }
        CTC_BIT_SET(flex_key->ipv6_sa_bmp, loop-SYS_ACL_FIELD_IPV6_SA_CHUNK0);
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_IPV6_SA);
    }while( (++loop) < (SYS_ACL_FIELD_IPV6_SA_CHUNK3+1) );

    loop = SYS_ACL_FIELD_XDATA_DW0;
    do
    {
        if (CTC_ACL_KSET_ISSET(*kset, loop))
        {
            SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_XDATA);
            break;
        }
    }while( loop++ < SYS_ACL_FIELD_STK_XDATA_B3 );

    if (CTC_ACL_KSET_ISSET(*kset, SYS_ACL_FIELD_FULL_ETHER_TYPE))
    {
        SYS_ACL_PKSET_ADD(p_kset, CTC_FIELD_KEY_ETHER_TYPE);
    }
    else if (CTC_ACL_KSET_ISSET(*kset, CTC_FIELD_KEY_ETHER_TYPE))
    {
        flex_key->compress_ether_type = TRUE;
    }
}

int32
_sys_tmm_acl_build_extract_db(uint8 lchip, uint8 size_mode, sys_acl_flex_key_t *fk)
{
    int32  ret = CTC_E_NONE;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    if (size_mode == SYS_ACL_FK_SIZE_MODE_AUTO)
    {
        ret = sys_tmm_acl_flex_key_assign_sel_code(lchip, SYS_ACL_FK_SIZE_MODE_SINGLE, fk);
        if (ret == CTC_E_NONE)
        {
            return ret;
        }

        ret = sys_tmm_acl_flex_key_assign_sel_code(lchip, SYS_ACL_FK_SIZE_MODE_DOUBLE, fk);
        if (ret == CTC_E_NONE)
        {
            return ret;
        }

        ret = sys_tmm_acl_flex_key_assign_sel_code(lchip, SYS_ACL_FK_SIZE_MODE_TRIPLE, fk);
    }
    else
    {
        ret = sys_tmm_acl_flex_key_assign_sel_code(lchip, size_mode, fk);
    }

    return ret;
}

int32
_sys_tmm_acl_fk_alloc_glb_ltid(uint8 lchip, uint8 dir, sys_acl_flex_key_t* fk)
{
    uint8  ltid = 0;
    uint8  acl_prio = 0;
    uint8  block_base = (dir == CTC_INGRESS ? 0 : ACL_IGS_BLOCK_MAX_NUM);
    uint8  max_block_num = (dir == CTC_INGRESS ? ACL_IGS_BLOCK_MAX_NUM : ACL_EGS_BLOCK_MAX_NUM);
    uint8  ltid_free_cnt = 0;
    sys_acl_lt_block_t *p_ltb = NULL;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "dir:%u\n", dir);

    for (ltid = SYS_ACL_INVALID_LTID+1; ltid < SYS_ACL_KEY_LTID_RSV_BASE; ltid++)
    {
        ltid_free_cnt = 0;
        SYS_ACL_IGS_BLK_LOOP(acl_prio, max_block_num)
        {
            p_ltb = &SYS_ACL_LT_BLOCK(lchip, block_base + acl_prio);
            if (CTC_IS_BIT_SET(p_ltb->ltid_bitmap, ltid))
            {
                break;
            }
            ltid_free_cnt++;
        }
        if (ltid_free_cnt == max_block_num)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "alloc ltid:%u\n", ltid);
            break;  /*found*/
        }
    }
    if (ltid == SYS_ACL_KEY_LTID_RSV_BASE)
    {
        return CTC_E_NO_RESOURCE;
    }
    SYS_ACL_IGS_BLK_LOOP(acl_prio, max_block_num)
    {
        p_ltb = &SYS_ACL_LT_BLOCK(lchip, block_base + acl_prio);
        CTC_BIT_SET(p_ltb->ltid_bitmap, ltid);
        CTC_BIT_SET(fk->ltid_valid, acl_prio);
        fk->ltid[acl_prio] = ltid;
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_fk_free_glb_ltid(uint8 lchip, uint8 dir, sys_acl_flex_key_t* fk)
{
    sys_acl_lt_block_t *p_ltb = NULL;
    uint8  acl_prio = 0;
    uint8  block_base = (dir == CTC_INGRESS ? 0 : ACL_IGS_BLOCK_MAX_NUM);
    uint8  max_block_num = (dir == CTC_INGRESS ? ACL_IGS_BLOCK_MAX_NUM : ACL_EGS_BLOCK_MAX_NUM);

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "dir: %u\n", dir);

    SYS_ACL_IGS_BLK_LOOP(acl_prio, max_block_num)
    {
        p_ltb = &SYS_ACL_LT_BLOCK(lchip, acl_prio+block_base);
        CTC_BIT_UNSET(p_ltb->ltid_bitmap, fk->ltid[acl_prio]);
        CTC_BIT_UNSET(fk->ltid_valid, acl_prio);
        fk->ltid[acl_prio] = SYS_ACL_INVALID_LTID;
    }
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "free fg->ltid:%d\n", fk->ltid[0]);
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_fk_alloc_ltid(uint8 lchip, uint8 block_id, sys_acl_flex_key_t* fk, uint8 from_position)
{
    uint8  loop = 0;
    uint8  acl_prio = SYS_ACL_BLOCK_PRIO(block_id);
    sys_acl_lt_block_t *p_ltb = NULL;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "block_id:%u\n", block_id);

    if (CTC_IS_BIT_SET(fk->ltid_valid, acl_prio))
    {
        return CTC_E_NONE;
    }
    p_ltb = &SYS_ACL_LT_BLOCK(lchip, block_id);
    if(from_position)
    {
        fk->ltid[acl_prio] = fk->ltid[acl_prio-1];
        CTC_BIT_SET(p_ltb->ltid_bitmap, fk->ltid[acl_prio-1]);
        CTC_BIT_SET(fk->ltid_valid, acl_prio);
        return CTC_E_NONE;
    }
    for (loop = SYS_ACL_INVALID_LTID+1; loop < SYS_ACL_KEY_LTID_RSV_BASE; loop++)
    {
        if (!CTC_IS_BIT_SET(p_ltb->ltid_bitmap, loop))
        {
            fk->ltid[acl_prio] = loop;
            CTC_BIT_SET(p_ltb->ltid_bitmap, loop);
            CTC_BIT_SET(fk->ltid_valid, acl_prio);
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "alloc ltid:%u, ltid_bitmap:0x%x\n", loop, p_ltb->ltid_bitmap);
            return CTC_E_NONE;
        }
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);
    return CTC_E_NO_RESOURCE;
}

int32
_sys_tmm_acl_fk_free_ltid(uint8 lchip, uint8 block_id, sys_acl_flex_key_t* fk)
{
    sys_acl_lt_block_t *p_ltb = NULL;
    uint8  acl_prio = SYS_ACL_BLOCK_PRIO(block_id);

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "block_id: %u\n", block_id);

    if (CTC_IS_BIT_SET(fk->ltid_valid, acl_prio))
    {
        p_ltb = &SYS_ACL_LT_BLOCK(lchip, block_id);
        CTC_BIT_UNSET(p_ltb->ltid_bitmap, fk->ltid[acl_prio]);
        CTC_BIT_UNSET(fk->ltid_valid, acl_prio);
        
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "free fg->ltid:%d, ltid_bitmap:0x%x\n", fk->ltid[acl_prio], p_ltb->ltid_bitmap);
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_presel_op_hw(uint8 lchip, sys_acl_tbl_info_t* p_tbl_info)
{
    uint8  block_id = SYS_ACL_BLOCK_PRIO(p_tbl_info->block_id);
    uint8  dir = SYS_ACL_BLOCK_DIR(p_tbl_info->block_id);
    uint16 hw_index = 0;
    uint32 cmd = 0;
    tbl_entry_t tcam_key;
    uint32 hw_table[CTC_BOTH_DIRECTION] = {DsLtidSelectTcam_t, DsEgrLtidSelectTcam_t};

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    tcam_key.data_entry = (uint32*)p_tbl_info->buffer[0];
    tcam_key.mask_entry = (uint32*)p_tbl_info->buffer[1];
    block_id = block_id + p_tbl_info->part;
    SYS_ACL_ENCODE_PRESEL_TCAM_INDEX(block_id, p_tbl_info->index, hw_index);
    if ((p_tbl_info->op_mode == SYS_ACL_OP_WRITE) || (p_tbl_info->op_mode == SYS_ACL_OP_BUILD))
    {
        cmd = DRV_IOW(hw_table[dir], DRV_ENTRY_FLAG);
    }
    else if (p_tbl_info->op_mode == SYS_ACL_OP_READ)
    {
        cmd = DRV_IOR(hw_table[dir], DRV_ENTRY_FLAG);
    }
    else if (p_tbl_info->op_mode == SYS_ACL_OP_DEL)
    {
        cmd = DRV_IOD(hw_table[dir], DRV_ENTRY_FLAG);
    }
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, hw_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &tcam_key));
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "write DsLtidSelectTcam_t :%d\n", hw_index);
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_fk_op_hw(uint8 lchip, uint8* ltid, sys_acl_tbl_info_t *p_tbl_info, sys_acl_flex_key_t *fk)
{
    uint8  block_id = SYS_ACL_BLOCK_PRIO(p_tbl_info->block_id);
    uint8  dir = SYS_ACL_BLOCK_DIR(p_tbl_info->block_id);
    uint32 cmd = 0;
    void*  p_action = NULL;
    DsProgramKeyGenProfile_m profile;
    uint32 profile_table[CTC_BOTH_DIRECTION] = {DsProgramKeyGenProfile0_t, DsEgrProgramKeyGenProfile0_t};
    uint32 ad_table[CTC_BOTH_DIRECTION] = {DsLtidSelectAd0_t, DsEgrLtidSelectAd0_t};

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    sal_memset(&profile, 0, sizeof(DsProgramKeyGenProfile_m));
    block_id = block_id + p_tbl_info->part;
    p_action = &profile;
    if (p_tbl_info->op_mode == SYS_ACL_OP_BUILD)
    {
        CTC_ERROR_RETURN(_sys_tmm_acl_fk_build_kgen_profile(lchip, p_tbl_info->part, fk, p_action));
        cmd = DRV_IOW(profile_table[dir] + block_id, DRV_ENTRY_FLAG);
    }
    else if (p_tbl_info->op_mode == SYS_ACL_OP_WRITE)
    {
        p_action = p_tbl_info->buffer[0];
        cmd = DRV_IOW(profile_table[dir] + block_id, DRV_ENTRY_FLAG);
    }
    else if (p_tbl_info->op_mode == SYS_ACL_OP_READ)
    {
        p_action = p_tbl_info->buffer[0];
        cmd = DRV_IOR(profile_table[dir] + block_id, DRV_ENTRY_FLAG);
    }
    else if (p_tbl_info->op_mode == SYS_ACL_OP_DEL)
    {
        cmd = DRV_IOD(profile_table[dir] + block_id, DRV_ENTRY_FLAG);
    }
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, p_tbl_info->index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), p_action));
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "Op %s  index:%d\n", DRV_TABLE_NAME(lchip, profile_table[dir] + block_id), p_tbl_info->index);

    p_action = p_tbl_info->buffer[1];
    if (p_tbl_info->op_mode == SYS_ACL_OP_BUILD)
    {
        CTC_ERROR_RETURN(_sys_tmm_acl_fk_build_presel_ad(lchip, p_tbl_info->part, *ltid, fk, p_action));
        cmd = DRV_IOW(ad_table[dir] + block_id, DRV_ENTRY_FLAG);
    }
    else if (p_tbl_info->op_mode == SYS_ACL_OP_WRITE)
    {
        cmd = DRV_IOW(ad_table[dir] + block_id, DRV_ENTRY_FLAG);
    }
    else if (p_tbl_info->op_mode == SYS_ACL_OP_READ)
    {
        cmd = DRV_IOR(ad_table[dir] + block_id, DRV_ENTRY_FLAG);
    }
    else if (p_tbl_info->op_mode == SYS_ACL_OP_DEL)
    {
        cmd = DRV_IOD(ad_table[dir] + block_id, DRV_ENTRY_FLAG);
    }
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, p_tbl_info->index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), p_action));
    if (p_tbl_info->op_mode == SYS_ACL_OP_READ)
    {
        *ltid = GetDsLtidSelectAd(V, ltId_f, p_action);
    }
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "Op %s  index:%d\n", DRV_TABLE_NAME(lchip, ad_table[dir] + block_id), p_tbl_info->index);
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_fk_write_action_priority(uint8 lchip, uint8 block_id, uint8 part, uint8 priority)
{
    uint32 cmd = 0;
    uint8  temp_block_id = 0;
    IpeAclGlobalActionPriorityCtl_m control;
    uint32  act_prio_table[2] = {IpeAclGlobalActionPriorityCtl_t, EpeAclGlobalActionPriorityCtl_t};
    uint32  block_base = SYS_ACL_BLOCK_BASE(block_id);
    uint8   dir = SYS_ACL_BLOCK_DIR(block_id);

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    temp_block_id = block_id + part - block_base;
    cmd = DRV_IOR(act_prio_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &control));

    SetIpeAclGlobalActionPriorityCtl(V, gLookup_0_actionPriority_f + temp_block_id, &control, (priority != 0xFF ? priority : temp_block_id));

    cmd = DRV_IOW(act_prio_table[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &control));
    return CTC_E_NONE;
}

int32
sys_tmm_acl_alloc_tcam_entry(uint8 lchip, void* pg_info, void* pe_info)
{
    uint8   block_id = 0;
    uint8   key_size = 0;
    uint8   block_base = 0;
    int32   ret = 0;
    sys_acl_block_t* pb = NULL;
    sys_acl_group_t* pg = pg_info;
    sys_acl_entry_t* pe = pe_info;
    /*sys_acl_flex_key_t *fk = NULL;*/

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    block_base = ( pg->group_info.dir==CTC_INGRESS? 0: ACL_IGS_BLOCK_MAX_NUM );
    block_id = pg->group_info.block_id + block_base;
    pb = &(SYS_ACL_BLOCK(lchip, block_id));
    key_size = pe->ua->buffer.fk->key_size;

    ret = sys_usw_acl_alloc_offset(lchip, pb, pe, key_size);
    if (CTC_E_NO_RESOURCE == ret)
    {
        return ret;
    }

    return CTC_E_NONE;

}

int32
sys_tmm_acl_free_tcam_entry(uint8 lchip, void* pg_info, void* pe_info)
{
    uint8   block_id = 0;
    sys_acl_block_t * pb        = NULL;
    sys_acl_group_t* pg = NULL;
    sys_acl_entry_t* pe = NULL;
    uint32 block_index = 0;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    pg = (sys_acl_group_t*) pg_info;
    pe = (sys_acl_entry_t*) pe_info;

    block_id = (pg->group_info.dir==CTC_INGRESS)?(pg->group_info.block_id):(pg->group_info.block_id+ACL_IGS_BLOCK_MAX_NUM);
    pb = &(SYS_ACL_BLOCK(lchip, block_id));
    block_index = pe->fpae.offset_a;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO,"Remove entry:%d form pb:%d, index:%d\n", pe->fpae.entry_id, pb->block_id, block_index);
    fpa_usw_free_offset(&pb->fpab, block_index);

    return CTC_E_NONE;
}


int32
_sys_tmm_acl_key_field_kset_add(uint8 key_type, uint32 field, sys_acl_kset_t* kset)
{

    switch(field)
    {
        /*not support*/
    case CTC_FIELD_KEY_TCP_OPTIONS:
    case CTC_FIELD_KEY_UDF_ENTRY_VALID:
    case CTC_FIELD_KEY_HASH_SEL_ID:
    case CTC_FIELD_KEY_HASH_VALID:
    case CTC_FIELD_KEY_CUSTOMER_ID:
    case CTC_FIELD_KEY_WLAN_RADIO_MAC:
    case CTC_FIELD_KEY_WLAN_RADIO_ID:
    case CTC_FIELD_KEY_WLAN_CTL_PKT:
    case CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT:
    case CTC_FIELD_KEY_IS_ESADI:
    case CTC_FIELD_KEY_IS_TRILL_CHANNEL:
    case CTC_FIELD_KEY_TRILL_INNER_VLANID:
    case CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID:
    case CTC_FIELD_KEY_TRILL_KEY_TYPE:
    case CTC_FIELD_KEY_TRILL_LENGTH:
    case CTC_FIELD_KEY_TRILL_MULTIHOP:
    case CTC_FIELD_KEY_NSH_NEXT_PROTOCOL:
    case CTC_FIELD_KEY_NSH_CBIT:
    case CTC_FIELD_KEY_NSH_OBIT:
    case CTC_FIELD_KEY_NSH_SI:
    case CTC_FIELD_KEY_NSH_SPI:
        SYS_ACL_PKSET_REMOVE(kset, field);
        break;

    default:
        SYS_ACL_PKSET_ADD(kset, field);
        break;
    }

    return 0;
}

STATIC int32
_sys_tmm_acl_dft_ltid_init(uint8 lchip)
{
    uint8  loop = 0;
    uint8  step = 0;
    uint32 cmd = 0;
    IpeFwdAclCtl_m acl_ctl;
    IpeCflexAclCtl_m cf_ctl;
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    sal_memset(&acl_ctl, 0, sizeof(acl_ctl));

    cmd = DRV_IOR(IpeFwdAclCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, cmd, &acl_ctl));

    SetIpeFwdAclCtl(V, l2LtidIndex_f            , &acl_ctl, SYS_ACL_KEY_LTID_MAC            );
    SetIpeFwdAclCtl(V, l3BasicLtidIndex_f       , &acl_ctl, SYS_ACL_KEY_LTID_IPV4           );
    SetIpeFwdAclCtl(V, ipv6BasicLtidIndex_f     , &acl_ctl, SYS_ACL_KEY_LTID_IPV6           );
    SetIpeFwdAclCtl(V, l3ExtLtidIndex_f         , &acl_ctl, SYS_ACL_KEY_LTID_IPV4_EXT       );
    SetIpeFwdAclCtl(V, l2L3BasicLtidIndex_f     , &acl_ctl, SYS_ACL_KEY_LTID_MAC_IPV4       );
    SetIpeFwdAclCtl(V, l2L3ExtLtidIndex_f       , &acl_ctl, SYS_ACL_KEY_LTID_MAC_IPV4_EXT   );
    SetIpeFwdAclCtl(V, ipv6ExtLtidIndex_f       , &acl_ctl, SYS_ACL_KEY_LTID_IPV6_EXT       );
    SetIpeFwdAclCtl(V, l2Ipv6ExtLtidIndex_f     , &acl_ctl, SYS_ACL_KEY_LTID_MAC_IPV6       );
    SetIpeFwdAclCtl(V, cidLtidIndex_f           , &acl_ctl, SYS_ACL_KEY_LTID_CID            );
    SetIpeFwdAclCtl(V, vlanLtidIndex_f          , &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE      );
    SetIpeFwdAclCtl(V, forwardBasicLtidIndex_f  , &acl_ctl, SYS_ACL_KEY_LTID_FWD            );
    SetIpeFwdAclCtl(V, forwardExtLtidIndex_f    , &acl_ctl, SYS_ACL_KEY_LTID_FWD_EXT        );
    SetIpeFwdAclCtl(V, coppBasicLtidIndex_f     , &acl_ctl, SYS_ACL_KEY_LTID_COPP           );
    SetIpeFwdAclCtl(V, coppExtLtidIndex_f       , &acl_ctl, SYS_ACL_KEY_LTID_COPP_EXT       );
    SetIpeFwdAclCtl(V, udfLtidIndex_f           , &acl_ctl, SYS_ACL_KEY_LTID_UDF            );
    /*SetIpeFwdAclCtl(V, vlanLtidIndex_f          , &acl_ctl, SYS_ACL_KEY_LTID_VLAN           );*/

    /*Interface key lt id default*/
    SetIpeFwdAclCtl(V, gL3Type_0_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_1_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_2_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_3_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_4_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_5_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_6_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_7_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_8_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_9_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_10_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_11_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_12_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_13_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_14_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);
    SetIpeFwdAclCtl(V, gL3Type_15_userDefinedLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_INTERFACE);

    cmd = DRV_IOW(IpeFwdAclCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, cmd, &acl_ctl));

    if (DRV_FROM_AT(lchip))
    {
        cmd = DRV_IOR(EpeAclQosCtl_t, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, cmd, &acl_ctl));

        SetEpeAclQosCtl(V, forwardBasicLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_FWD);
        SetEpeAclQosCtl(V, forwardExtLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_FWD_EXT);
        SetEpeAclQosCtl(V, ipv6BasicLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_IPV6);
        SetEpeAclQosCtl(V, ipv6ExtLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_IPV6_EXT);
        SetEpeAclQosCtl(V, l2Ipv6ExtLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_MAC_IPV6);
        SetEpeAclQosCtl(V, l2L3BasicLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_MAC_IPV4);
        SetEpeAclQosCtl(V, l2L3ExtLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_MAC_IPV4_EXT);
        SetEpeAclQosCtl(V, l2LtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_MAC);
        SetEpeAclQosCtl(V, l3BasicLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_IPV4);
        SetEpeAclQosCtl(V, l3ExtLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_IPV4_EXT);
        /*SetEpeAclQosCtl(V, vlanLtidIndex_f, &acl_ctl, SYS_ACL_KEY_LTID_VLAN);*/

        cmd = DRV_IOW(EpeAclQosCtl_t, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, cmd, &acl_ctl));
    }

    cmd = DRV_IOR(IpeCflexAclCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, cmd, &cf_ctl));

    step = IpeCflexAclCtl_cflexAcllkpLevel_1_dftLtidforCflex_f - IpeCflexAclCtl_cflexAcllkpLevel_0_dftLtidforCflex_f;

    for (loop = 0; loop < ACL_IGS_BLOCK_MAX_NUM; loop++)
    {
        SetIpeCflexAclCtl(V, cflexAcllkpLevel_0_dftLtidforCflex_f + loop*step, &cf_ctl, SYS_ACL_KEY_LTID_STK_HDR);
    }

    cmd = DRV_IOW(IpeCflexAclCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, cmd, &cf_ctl));


   return CTC_E_NONE;
}

#if 0
int32
sys_tmm_acl_set_fixed_kset(uint8 lchip, ctc_acl_fixed_kset_t *p_fix_kset)
{
    sys_acl_flex_group_t *fg;
    uint8 ltid = 0;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    CTC_PTR_VALID_CHECK(p_fix_kset);
    CTC_PTR_VALID_CHECK(p_fix_kset->kset);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "priority = %d,  key_type=%d\n", p_fix_kset->priority, p_fix_kset->key_type);
    CTC_MAX_VALUE_CHECK(p_fix_kset->priority, p_usw_acl_master[lchip]->igs_block_num-1);

    switch(p_fix_kset->key_type)
    {
    case CTC_ACL_KEY_MAC:
        ltid = SYS_ACL_KEY_LTID_MAC;
        break;

    case CTC_ACL_KEY_IPV4:
        ltid = SYS_ACL_KEY_LTID_IPV4;
        break;

    case CTC_ACL_KEY_IPV4_EXT:
        ltid = SYS_ACL_KEY_LTID_IPV4_EXT;
        break;

    case CTC_ACL_KEY_MAC_IPV4:
        ltid = SYS_ACL_KEY_LTID_MAC_IPV4;
        break;

    case CTC_ACL_KEY_MAC_IPV4_EXT:
        ltid = SYS_ACL_KEY_LTID_MAC_IPV4_EXT;
        break;

    case CTC_ACL_KEY_IPV6:
        ltid = SYS_ACL_KEY_LTID_IPV6;
        break;

    case CTC_ACL_KEY_IPV6_EXT:
        ltid = SYS_ACL_KEY_LTID_IPV6_EXT;
        break;

    case CTC_ACL_KEY_MAC_IPV6:
        ltid = SYS_ACL_KEY_LTID_MAC_IPV6;
        break;

    case CTC_ACL_KEY_CID:
        ltid = SYS_ACL_KEY_LTID_CID;
        break;

    case CTC_ACL_KEY_INTERFACE:
        ltid = SYS_ACL_KEY_LTID_INTERFACE;
        break;

    case CTC_ACL_KEY_FWD:
        ltid = SYS_ACL_KEY_LTID_FWD;
        break;

    case CTC_ACL_KEY_FWD_EXT:
        ltid = SYS_ACL_KEY_LTID_FWD_EXT;
        break;

    case CTC_ACL_KEY_COPP:
        ltid = SYS_ACL_KEY_LTID_COPP;
        break;

    case CTC_ACL_KEY_COPP_EXT:
        ltid = SYS_ACL_KEY_LTID_COPP_EXT;
        break;

    case CTC_ACL_KEY_UDF:
        ltid = SYS_ACL_KEY_LTID_UDF;
        break;

    case CTC_ACL_KEY_STK_HDR:
        ltid = SYS_ACL_KEY_LTID_STK_HDR;
        break;

    default:
        CTC_ERROR_RETURN(CTC_E_INVALID_CONFIG);
    }

    fg = p_usw_acl_master[lchip]->p_fixed_fg_user + p_fix_kset->priority*CTC_ACL_KEY_NUM + p_fix_kset->key_type;

    if (p_fix_kset->valid)
    {
        sal_memset(fg, 0, sizeof(sys_acl_flex_group_t));
        sal_memcpy(&fg->kset, p_fix_kset->kset, sizeof(ctc_acl_kset_t));

        _sys_tmm_acl_group_update_kset(lchip, &fg->kset);

        fg->mode = p_fix_kset->mode;

        fg->ltid = ltid;

        CTC_ERROR_RETURN(_sys_tmm_acl_group_get_sel_codes(lchip, fg));

        p_fix_kset->mode = fg->mode;
    }
    else
    {
        fg->ltid = 0;
    }

    return 0;
}


int32
sys_tmm_acl_get_fixed_kset(uint8 lchip, ctc_acl_fixed_kset_t *p_fix_kset)
{
    sys_acl_flex_group_t *fg;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    CTC_PTR_VALID_CHECK(p_fix_kset);
    CTC_PTR_VALID_CHECK(p_fix_kset->kset);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "priority = %d,  key_type=%d\n", p_fix_kset->priority, p_fix_kset->key_type);


    if (p_usw_acl_master[lchip]->p_fixed_fg_user == NULL)
    {
        return 0;
    }

    if (p_fix_kset->priority >= p_usw_acl_master[lchip]->igs_block_num)
    {
        return CTC_E_INVALID_CONFIG;
    }

    fg = p_usw_acl_master[lchip]->p_fixed_fg_user + p_fix_kset->priority*CTC_ACL_KEY_NUM + p_fix_kset->key_type;



    sal_memcpy(p_fix_kset->kset, &fg->kset, sizeof(ctc_acl_kset_t));

    p_fix_kset->mode = fg->mode;
    p_fix_kset->valid = fg->ltid ?1:0;

    return 0;

}
#endif

STATIC int32
_sys_tmm_acl_get_offset(uint8 lchip,
                   uint16 qid,
                   sys_acl_flex_key_t *p_flex_key,
                   sys_acl_field_offset_info_t* p_offset,
                   uint32 *p_width)
{
    uint16 i = 0;
    uint8 part = 0;
    uint8 part_num = 0;
    uint32 width = 0;
    sys_acl_group_field_offset_info_t *group_field = NULL;
    sys_acl_field_offset_info_t *p_offset_arr[3] = {NULL};

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    part_num = p_flex_key->key_size;

    /*found the qid*/
    for (part = 0; part < part_num; part++)
    {
        group_field = &(p_flex_key->field_arr[part]);

        for (i = 0; i < group_field->size; i++)
        {
            if (qid == group_field->qid_arr[i])
            {
                p_offset_arr[part] = &group_field->offset_arr[i];

                break;
            }
        }
    }

    if (NULL == p_offset_arr[0] &&
        NULL == p_offset_arr[1] &&
    NULL == p_offset_arr[2])
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
        return CTC_E_NOT_SUPPORT;
    }


    /*calculate the width*/
    for (part = 0; part < part_num; part++)
    {

        if (NULL == p_offset_arr[part])
        {
            continue;
        }

        for (i = 0; i < p_offset_arr[part]->num_offsets; i++)
        {
            if (0 == p_offset_arr[part]->width[i])
            {
                continue;
            }

            p_offset->offset[i] = p_offset_arr[part]->offset[i] + part*160;
            p_offset->width[i] = p_offset_arr[part]->width[i];
            p_offset->num_offsets++;

            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "qid:%d %s offset[%d]: %d, width:%d\n",
                            qid, ifp_field_str[qid], i,
                            p_offset->offset[i], p_offset->width[i]);

            width += p_offset_arr[part]->width[i];
        }
    }


    *p_width = width;

    return 0;
}

STATIC int32
_sys_tmm_acl_get_range_value(uint8 lchip,
                              sys_acl_entry_t* pe,
                              uint16 qid,
                              uint16 *min,
                              uint16 *max)
{
    uint8 type = 0;
    uint8 type_2 = 0;
    uint8 range_id = 0;
    sys_acl_field_range_t* p_range = NULL;

    p_range = p_usw_acl_master[lchip]->field_range[1+pe->group->group_info.dir];

    switch(qid)
    {
    case CTC_FIELD_KEY_SVLAN_RANGE:
        type = ACL_RANGE_TYPE_SVLAN;
        break;

    case CTC_FIELD_KEY_CVLAN_RANGE:
        type = ACL_RANGE_TYPE_CVLAN;
        break;

    case CTC_FIELD_KEY_IP_PKT_LEN_RANGE:
        type = ACL_RANGE_TYPE_PKT_LENGTH;
        break;

    case CTC_FIELD_KEY_L4_SRC_PORT_RANGE:
        type = ACL_RANGE_TYPE_L4_SRC_PORT;
        break;

    case CTC_FIELD_KEY_L4_DST_PORT_RANGE:
        type = ACL_RANGE_TYPE_L4_DST_PORT;
        break;

    case CTC_FIELD_KEY_UDF_RANGE:   /* may udf_range or sudf_range*/
        type = ACL_RANGE_TYPE_UDF;
        type_2 = ACL_RANGE_TYPE_SUDF;
        break;
	case CTC_FIELD_KEY_SUDF_RANGE:
        type = ACL_RANGE_TYPE_SUDF;
        break;

    default:
        break;
    }

    do{
        if (!CTC_IS_BIT_SET(pe->un.g4.range_bitmap, range_id))
        {
            continue;
        }
        if (p_range->range[range_id].range_type == type || p_range->range[range_id].range_type == type_2)
        {
            *min = p_range->range[range_id].min;
            *max = p_range->range[range_id].max;
            break;
        }
    }while(++range_id < SYS_ACL_FIELD_RANGE_NUM);

    if(SYS_ACL_FIELD_RANGE_NUM == range_id)
    {
        return CTC_E_NOT_EXIST;
    }

    return 0;
}


STATIC int32
_sys_tmm_acl_get_range_bitmap(uint8 lchip,
                              sys_acl_entry_t* pe,
                              ctc_field_key_t* pKey,
                              uint8 is_add,
                              uint32* p_value,
                              uint32* p_mask)
{
    int32  ret = 0;
    uint32 qid = 0;
    uint8 type = 0;
    uint16 range_bmp = pe->un.g4.range_bitmap;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    qid = pKey->type;

    switch(qid)
    {
    case CTC_FIELD_KEY_SVLAN_RANGE:
        type = ACL_RANGE_TYPE_SVLAN;
        break;

    case CTC_FIELD_KEY_CVLAN_RANGE:
        type = ACL_RANGE_TYPE_CVLAN;
        break;

    case CTC_FIELD_KEY_IP_PKT_LEN_RANGE:
        type = ACL_RANGE_TYPE_PKT_LENGTH;
        break;

    case CTC_FIELD_KEY_L4_SRC_PORT_RANGE:
        type = ACL_RANGE_TYPE_L4_SRC_PORT;
        break;

    case CTC_FIELD_KEY_L4_DST_PORT_RANGE:
        type = ACL_RANGE_TYPE_L4_DST_PORT;
        break;
    case CTC_FIELD_KEY_SUDF_RANGE:
        type = ACL_RANGE_TYPE_SUDF;
        break;
    case CTC_FIELD_KEY_UDF_RANGE:
        type = ACL_RANGE_TYPE_UDF;
        break;

    default:
        break;
    }

    if (is_add)
    {
        sys_parser_range_op_ctl_t range_ctl;
        sal_memset(&range_ctl, 0, sizeof(range_ctl));

        if (type == ACL_RANGE_TYPE_UDF)
        {
            ctc_acl_udf_t *p_udf = pKey->ext_data;
            sys_acl_udf_entry_t *p_udf_entry = NULL;

            CTC_PTR_VALID_CHECK(p_udf);
            CTC_MAX_VALUE_CHECK(p_udf->sel_id, 7);

            if (!p_udf->udf_ad_id)
            {
                _sys_usw_acl_get_udf_info(lchip, p_udf->udf_id, &p_udf_entry);
                if (!p_udf_entry || !p_udf_entry->key_index_used )
                {
                    return CTC_E_NOT_EXIST;
                }
                range_ctl.udf_index = p_udf_entry->udf_hit_index;
                range_ctl.is_sudf = p_udf_entry->is_sudf;
            }
            else
            {
                CTC_VALUE_RANGE_CHECK(p_udf->udf_ad_id, MCHIP_CAP(SYS_CAP_ACL_UDF_AD_ID_MIN), MCHIP_CAP(SYS_CAP_ACL_UDF_AD_ID_MAX));
                CTC_ERROR_RETURN(_sys_usw_acl_get_udf_entry_by_ad_id(lchip, p_udf->udf_ad_id, &p_udf_entry));
                if (!p_udf_entry)
                {
                    return CTC_E_NOT_EXIST;
                }
                range_ctl.udf_index = p_udf->udf_ad_id;
                range_ctl.is_sudf = 0;
            }

            range_ctl.min_value = p_udf->min;
            range_ctl.max_value = p_udf->max;
            range_ctl.udf_sel = p_udf_entry->granularity ? p_udf->sel_id : ((p_udf->sel_id%2) ? (p_udf->sel_id - 1) : (p_udf->sel_id + 1));
            range_ctl.udf_mask = p_udf->mask;
            range_ctl.type = range_ctl.is_sudf ? ACL_RANGE_TYPE_SUDF : type;
        }
        else if (type == ACL_RANGE_TYPE_SUDF)
        {
            ctc_acl_udf_t *p_udf = pKey->ext_data;
            sys_acl_udf_entry_t *p_udf_entry = NULL;
            uint8 granularity = 1;

            CTC_PTR_VALID_CHECK(p_udf);
            CTC_MAX_VALUE_CHECK(p_udf->sel_id, 7);

            if (!p_udf->udf_ad_id)
            {
                _sys_usw_acl_get_udf_info(lchip, p_udf->udf_id, &p_udf_entry);
                if (!p_udf_entry || !p_udf_entry->key_index_used )
                {
                    return CTC_E_NOT_EXIST;
                }
                if (!p_udf_entry->is_sudf)
                {
                    return CTC_E_PARAM_CONFLICT;
                }
                range_ctl.udf_index = p_udf_entry->udf_hit_index;
                granularity = p_udf_entry->granularity;
            }
            else
            {
                CTC_VALUE_RANGE_CHECK(p_udf->udf_ad_id + MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MIN) -1, MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MIN), MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MAX));
                range_ctl.udf_index = p_udf->udf_ad_id + MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MIN) -1;
                if(range_ctl.udf_index != p_usw_acl_master[lchip]->srv6_rsv_udf_idx)
                {
                    _sys_usw_acl_get_udf_entry_by_ad_id(lchip, range_ctl.udf_index, &p_udf_entry);
                    if(!p_udf_entry)
                    {
                        return CTC_E_NOT_EXIST;
                    }
                    granularity = p_udf_entry->granularity;
                }
            }
            range_ctl.is_sudf = 1;
            range_ctl.min_value = p_udf->min;
            range_ctl.max_value = p_udf->max;
            range_ctl.udf_sel = granularity ? p_udf->sel_id : ((p_udf->sel_id%2) ? (p_udf->sel_id - 1) : (p_udf->sel_id + 1));
            range_ctl.udf_mask = p_udf->mask;
            range_ctl.type = type;
        }
        else
        {
            range_ctl.min_value = pKey->data;
            range_ctl.max_value = pKey->mask;
            range_ctl.type = type;
        }

        range_ctl.type = type;
        range_ctl.dir = pe->group->group_info.dir;
        range_ctl.is_program = 1;

        ret = __sys_usw_acl_add_field_range(lchip, &range_ctl, &range_bmp);
        if (0 != ret)
        {
            return ret;
        }
    }
    else
    {
        CTC_ERROR_RETURN(_sys_usw_acl_remove_field_range(lchip, pe->group->group_info.dir, type, 1, &range_bmp));
    }

    *p_value = range_bmp;
    *p_mask  = range_bmp;
    pe->un.g4.range_bitmap = range_bmp;
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "range_bitmap:0x%X\n", pe->un.g4.range_bitmap);

    return 0;
}

int32
sys_tmm_acl_add_compress_ether_type(uint8 lchip, uint8 is_global, uint8 dir, uint16 new_ether_type, uint16 old_ether_type,uint8* o_cether_type, uint8* o_cether_type_index)
{
    uint32 cmd = 0;
    uint32 ceth_type[CTC_BOTH_DIRECTION] = {ProgramAclEtherTypeCompressor_t, EgrProgramAclEtherTypeCompressor_t};
    ProgramAclEtherTypeCompressor_m    cam_cethertype;
    sys_register_cethertype_t new_cethertype_t = {0};
    sys_register_cethertype_t old_cethertype_t = {0};
    sys_register_cethertype_t*    pcether_get  = NULL;

    new_cethertype_t.ether_type = new_ether_type;
    new_cethertype_t.index.index = *o_cether_type_index;
    old_cethertype_t.ether_type = old_ether_type;
    SYS_ACL_INIT_CHECK();
    CTC_ERROR_RETURN(ctc_spool_add(p_usw_acl_master[lchip]->cethertype_spool[!!is_global][dir],&new_cethertype_t,&old_cethertype_t, &pcether_get));
    if(o_cether_type)
    {   
        sal_memset(&cam_cethertype, 0,sizeof(cam_cethertype));
        *o_cether_type = (1 << 6 | (pcether_get->index.index&0x3F));
        SetProgramAclEtherTypeCompressor(V, etherType_f, &cam_cethertype, pcether_get->ether_type);
        SetProgramAclEtherTypeCompressor(V, compressEtherType_f, &cam_cethertype, pcether_get->index.index);
        SetProgramAclEtherTypeCompressor(V, valid_f, &cam_cethertype, 1);
        cmd = DRV_IOW(ceth_type[dir], DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, pcether_get->index.index, (DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !is_global)), &cam_cethertype));
    }
    *o_cether_type_index = pcether_get->index.index & 0x3F;
    return CTC_E_NONE;
}

int32
sys_tmm_acl_remove_compress_ether_type(uint8 lchip, uint8 is_global, uint8 dir, uint8 ether_type_index)
{
    uint16 ether_type = 0;
    uint32 cmd = 0;
    uint32 ceth_type[CTC_BOTH_DIRECTION] = {ProgramAclEtherTypeCompressor_t, EgrProgramAclEtherTypeCompressor_t};
    sys_register_cethertype_t cethertype_t = {0};
    ProgramAclEtherTypeCompressor_m cam_cethertype;

    sal_memset(&cam_cethertype, 0 , sizeof(ProgramAclEtherTypeCompressor_m));

    SYS_ACL_INIT_CHECK();

    cmd = DRV_IOR(ceth_type[dir], DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, ether_type_index, (DRV_CMD_PP_EN(DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !is_global))), &cam_cethertype));
    ether_type = GetProgramAclEtherTypeCompressor(V,etherType_f, &cam_cethertype);

    cethertype_t.ether_type = ether_type;

    CTC_ERROR_RETURN(ctc_spool_remove(p_usw_acl_master[lchip]->cethertype_spool[!!is_global][dir], &cethertype_t, NULL));
    return CTC_E_NONE;
}

int32
sys_tmm_acl_build_qid_field(uint8 lchip,
                            sys_acl_entry_t* pe,
                            uint16 qid,
                            uint32* p_data,
                            uint32* p_mask,
                            uint8 is_add)
{
    sys_acl_field_offset_info_t offset;
    sys_acl_field_offset_info_t *p_offset = &offset;
    sys_acl_kset_t kset;
    uint16 offset_src = 0;
    uint16 offset_dst = 0;
    uint32 width = 0;
    uint16 i = 0;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    sal_memset(&offset, 0, sizeof(offset));

    sal_memcpy(&kset,&(pe->ua->buffer.fk->kset),sizeof(sys_acl_kset_t));


    CTC_ERROR_RETURN(_sys_tmm_acl_get_offset(lchip,
                                            qid,
                                            pe->ua->buffer.fk,
                                            &offset,
                                            &width));

    if (width <= 32)
    {
        if (width == 1)
        {
            (*p_data) = (*p_data)?1:0;
            (*p_mask) = (*p_mask)?1:0;
        }
        else
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "param:0x%x, width:%d\n", *p_data, width);
            CTC_MAX_VALUE_CHECK((*p_data), (1 << (width - 1)) + (1 << (width - 1)) - 1);
        }
    }

    /*copy data/mask*/
    for (i = 0; i < p_offset->num_offsets; i++)
    {
        width = p_offset->width[i];
        offset_dst = p_offset->offset[i];

        if (is_add)
        {
            CTC_BMP_COPY_RANGE(pe->ua->buffer.key, offset_dst, p_data, offset_src,  width);
            CTC_BMP_COPY_RANGE(pe->ua->buffer.mask, offset_dst, p_mask, offset_src, width);
            offset_src += p_offset->width[i];

            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "Qid %s copy dst offst:%d, src_offset:%d, width:%d\n",
                            ifp_field_str[qid], offset_dst, offset_src, width);
        }
        else
        {
            CTC_BMP_CLR_RANGE(pe->ua->buffer.key, offset_dst, width);
            CTC_BMP_CLR_RANGE(pe->ua->buffer.mask, offset_dst, width);
        }
    }


    return 0;
}

STATIC int32
_sys_tmm_acl_check_field_union(uint8 lchip, ctc_field_key_t* key_field, sys_acl_entry_t* pe)
{
    uint32 cmd = 0;
    ds_t  ds;
    uint8 l3_type = 0;
    uint8 l4_type = 0;
    uint8 l4_user_type = 0;
    uint8 gre_valid = 0, gre_key_valid = 0;
    uint8 arp_target_mac = 0, arp_sender_mac = 0;
    uint8 xgpon_use_gem_port = 0;
    uint8 merge_data_mode = 0;   /*bit0-vxlan vni valid, 1-gre no key valid, 2-gre key valid, 3-use mac da*/
    uint8 dir = pe->group->group_info.dir;
    uint32 tbl_id = 0;
    sys_acl_flex_key_t* fk = pe->ua->buffer.fk;

    tbl_id = (CTC_INGRESS == pe->group->group_info.dir ? DsLtidSelectAd0_t : DsEgrLtidSelectAd0_t);
    cmd = DRV_IOR(tbl_id+pe->group->group_info.block_id, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, fk->ad_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
    /*port_use_meta = GetDsLtidSelectAd(V, fsSrcInfo0IsMetadataShare_f, ds);*/
    gre_valid = GetDsLtidSelectAd(V, fs16L4ByteGreShare_f, ds);
    gre_key_valid = GetDsLtidSelectAd(V, fs16L4ByteGreWithKeyShare_f, ds);
    arp_target_mac = GetDsLtidSelectAd(V, gArpFsCtl_dMacSel_f, ds);
    arp_sender_mac = GetDsLtidSelectAd(V, gArpFsCtl_sMacSel_f, ds);

    if (dir == CTC_INGRESS)
    {
        cmd = DRV_IOR(IpeAclQosCtl_t, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
        xgpon_use_gem_port = GetIpeAclQosCtl(V, useGemPortLookup_f, ds);
        cmd = DRV_IOR(ProgramTcamAclCtl_t, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
        merge_data_mode = GetProgramTcamAclCtl(V, fsMergeDataShareTypeCtl_f, ds);
    }
    else
    {
        cmd = DRV_IOR(EgrProgramTcamAclCtl_t, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
        merge_data_mode = GetProgramTcamAclCtl(V, fsMergeDataShareTypeCtl_f, ds);
    }

    /*
    cid_key = pe->key_type == CTC_ACL_KEY_CID;
    copp_ext_key = pe->key_type == CTC_ACL_KEY_COPP_EXT;
    copp_basic_key = pe->key_type == CTC_ACL_KEY_COPP;
    copp_key = copp_basic_key || copp_ext_key;
    fwd_ext_key =  pe->key_type == CTC_ACL_KEY_FWD_EXT;
    fwd_key =  pe->key_type == CTC_ACL_KEY_FWD;
    l3_key = pe->key_type == CTC_ACL_KEY_IPV4;
    mac_key = pe->key_type == CTC_ACL_KEY_MAC;
    l3_ext_key = pe->key_type == CTC_ACL_KEY_IPV4_EXT;
    mac_l3_key = pe->key_type == CTC_ACL_KEY_MAC_IPV4;
    mac_l3_ext_key = pe->key_type == CTC_ACL_KEY_MAC_IPV4_EXT;
    short_key = pe->key_type == CTC_ACL_KEY_INTERFACE;
    udf_key = pe->key_type == CTC_ACL_KEY_UDF;
    ipv6_ext_key = pe->key_type == CTC_ACL_KEY_IPV6_EXT;
    */

    l3_type = pe->l3_type;
    l4_type = pe->l4_type;
    l4_user_type = pe->l4_user_type;

    if (!key_field->ext_data)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "Key filed:%d,keyData:%d\n", key_field->type, key_field->data)
    }
    else
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "Key filed:%d ", key_field->type)
    }

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "l2_type:%d l3_type:%d l4_type:%d,l4 userType:%d \n", pe->l2_type, pe->l3_type, pe->l4_type, pe->l4_user_type);

    switch(key_field->type)
    {
    case CTC_FIELD_KEY_GEM_PORT:
        if (!xgpon_use_gem_port)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Gem port not support \n");
            return CTC_E_NOT_SUPPORT;
        }
        break;
    case CTC_FIELD_KEY_METADATA:
        CTC_MAX_VALUE_CHECK(key_field->data, 0x3FFF);
        break;
    case CTC_FIELD_KEY_MAC_SA:                   /**<  Source MAC Address. */
        if(pe->flag & SYS_ACL_ENTRY_FLAG_TUNNEL_INFO)
        {
            return CTC_E_IN_USE;
        }
    case CTC_FIELD_KEY_MAC_DA:                   /**<  Destination MAC Address. */
        if (((key_field->type == CTC_FIELD_KEY_MAC_SA) && arp_sender_mac && l3_type == CTC_PARSER_L3_TYPE_ARP) ||
            ((key_field->type == CTC_FIELD_KEY_MAC_DA) && arp_target_mac && l3_type == CTC_PARSER_L3_TYPE_ARP))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "  DsLtidSelectAd.gArpFsCtl.%s is 1\n", arp_target_mac? "dMacSel": "sMacSel");
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_L4_USER_TYPE:             /**<  Layer 4 Usertype (ctc_parser_l4_usertype_t). */
        if (CTC_PARSER_L4_TYPE_NONE == l4_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Current L4 Type :%d \n", l4_type);
            return CTC_E_INVALID_PARAM;
        }
        break;
    case CTC_FIELD_KEY_IP_SA:                    /**<  Source IPv4 Address. */
    case CTC_FIELD_KEY_IP_DA:
        if (l3_type != CTC_PARSER_L3_TYPE_IPV4)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 Type is not Ipv4, L3 Type :%d \n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;


    case CTC_FIELD_KEY_IPV6_SA:                  /**<  Source IPv6 Address. */
    case CTC_FIELD_KEY_IPV6_DA:                  /**<  Destination IPv6 Address. */
    case CTC_FIELD_KEY_IPV6_FLOW_LABEL:          /**<  Ipv6 Flow label*/
        if (l3_type != CTC_PARSER_L3_TYPE_IPV6)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 Type is not Ipv6, L3 Type :%d \n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_IP_TTL:
    case CTC_FIELD_KEY_IP_DSCP:                  /**<  DSCP. */
    case CTC_FIELD_KEY_IP_PKT_LEN_RANGE:
    case CTC_FIELD_KEY_IP_PROTOCOL:              /**<  Ip Protocol. */
    case CTC_FIELD_KEY_IP_PRECEDENCE:            /**<  Precedence. */
    case CTC_FIELD_KEY_IP_ECN:                   /**<  Ecn. */
    case CTC_FIELD_KEY_IP_FRAG:                  /**<  IP Fragment Information. */
    case CTC_FIELD_KEY_IP_HDR_ERROR:             /**<  Ip Header Error. */
    case CTC_FIELD_KEY_IP_OPTIONS:               /**<  Ip Options. */
        if (l3_type != CTC_PARSER_L3_TYPE_IPV4
            && l3_type != CTC_PARSER_L3_TYPE_IPV6)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 Type is not Ipv6 or ipv4, L3 Type :%d \n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_L4_DST_PORT:
    case CTC_FIELD_KEY_L4_SRC_PORT:
        if (CTC_PARSER_L3_TYPE_IPV4 != l3_type
                && CTC_PARSER_L3_TYPE_IPV6 != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 Type is not Ipv6 or ipv4, L3 Type :%d \n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        if (l4_type == CTC_PARSER_L4_TYPE_NONE
            || CTC_PARSER_L4_TYPE_ICMP == l4_type
        || CTC_PARSER_L4_TYPE_IGMP == l4_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L4 port's L4 Type is conflict, L4 Type :%d \n", l4_type);
            return CTC_E_PARAM_CONFLICT;
        }
        /*l4ByteVxlanShare = (l3IsIpv4 || l3IsIpv6) && vxlanShareValid && TempDsLtidSelectAd.fs16L4ByteVxlanShare;*/
        if(l4_type == CTC_PARSER_L4_TYPE_UDP && l4_user_type == CTC_PARSER_L4_USER_TYPE_UDP_VXLAN)
        {
            cmd = DRV_IOR(tbl_id+pe->group->group_info.block_id, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, fk->ad_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
            if(GetDsLtidSelectAd(V, fs16L4ByteVxlanShare_f, ds))
            {
                return CTC_E_PARAM_CONFLICT;
            }
        }
        break;

    case CTC_FIELD_KEY_L4_SRC_PORT_RANGE:        /**<  Layer 4 Src Port Range; data: min, mask: max. */
    case CTC_FIELD_KEY_L4_DST_PORT_RANGE:        /**<  Layer 4 Dest Port Range;  data: min, mask: max. */

        if (CTC_PARSER_L3_TYPE_IPV4 != l3_type
            && CTC_PARSER_L3_TYPE_IPV6 != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 Type is not Ipv6 or ipv4, L3 Type :%d \n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        if (l4_type == CTC_PARSER_L4_TYPE_NONE
            || CTC_PARSER_L4_TYPE_ICMP == l4_type
        || CTC_PARSER_L4_TYPE_IGMP == l4_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L4 port's L4 Type is conflict, L4 Type :%d \n", l4_type);
            return CTC_E_PARAM_CONFLICT;
        }
        break;

    case CTC_FIELD_KEY_TCP_ECN:                  /**<  TCP Ecn. */
    case CTC_FIELD_KEY_TCP_FLAGS:                /**<  TCP Flags (ctc_acl_tcp_flag_flag_t). */
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4  && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_TCP))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "TCP param is mismatch , l3_type :%d l4_type:%d \n", l3_type, l4_type);
            return CTC_E_INVALID_PARAM;
        }

        break;

    case CTC_FIELD_KEY_ICMP_CODE:
    case CTC_FIELD_KEY_ICMP_TYPE:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_ICMP))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Param is mismatch , l3_type :%d l4_type:%d \n", l3_type, l4_type);
            return CTC_E_INVALID_PARAM;
        }

        break;

    case CTC_FIELD_KEY_IGMP_TYPE:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_IGMP))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Param is mismatch , l3_type :%d l4_type:%d \n", l3_type, l4_type);
            return CTC_E_INVALID_PARAM;
        }

        break;

    case CTC_FIELD_KEY_VXLAN_FLAGS:
    case CTC_FIELD_KEY_VXLAN_RSV1:
    case CTC_FIELD_KEY_VXLAN_RSV2:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_UDP)
            || (l4_user_type != CTC_PARSER_L4_USER_TYPE_UDP_VXLAN))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Param is mismatch , l3_type :%d l4_type:%d \n", l3_type, l4_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_VN_ID:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_UDP)
        || (l4_user_type != CTC_PARSER_L4_USER_TYPE_UDP_VXLAN))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Param is mismatch , l3_type :%d l4_type:%d \n", l3_type, l4_type);
            return CTC_E_INVALID_PARAM;
        }

        break;
    case CTC_FIELD_KEY_GRE_KEY:                     /**< Gre Key. */
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_GRE))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "param is mismatch , l3_type :%d l4_type:%d \n", l3_type, l4_type);
            return CTC_E_INVALID_PARAM;
        }
        if (!gre_valid || !gre_key_valid)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Field gre-key not select or gre-flags, gre-protocol selected \n");
            return CTC_E_INVALID_PARAM;
        }
        break;
    case CTC_FIELD_KEY_NVGRE_KEY:                  /**<  NVGre Key. */
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_GRE))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "param is mismatch , l3_type :%d l4_type:%d \n", l3_type, l4_type);
            return CTC_E_INVALID_PARAM;
        }
        if (!gre_valid || !gre_key_valid)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Field gre-key not valid \n");
            return CTC_E_INVALID_PARAM;
        }
        break;
    case CTC_FIELD_KEY_GRE_PROTOCOL_TYPE:
    case CTC_FIELD_KEY_GRE_FLAGS:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_GRE))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "param is mismatch , l3_type :%d l4_type:%d \n", l3_type, l4_type);
            return CTC_E_INVALID_PARAM;
        }
        if (!gre_valid || gre_key_valid)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Field gre-no-key not valid \n");
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_ARP_OP_CODE:
    case CTC_FIELD_KEY_ARP_PROTOCOL_TYPE:
    case CTC_FIELD_KEY_ARP_SENDER_IP:
    case CTC_FIELD_KEY_ARP_TARGET_IP:
    case CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL:
    case CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL:
    case CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL:
    case CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL:
    case  CTC_FIELD_KEY_SENDER_MAC:                   /**<  Ethernet Address of sender of ARP Header */
    case  CTC_FIELD_KEY_TARGET_MAC:                   /**<  Ethernet Address of destination of ARP Header */
    case CTC_FIELD_KEY_GARP:
    case CTC_FIELD_KEY_RARP:
        if (CTC_PARSER_L3_TYPE_ARP != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not ARP l3 type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        if (((key_field->type == CTC_FIELD_KEY_SENDER_MAC) && (!arp_sender_mac || (pe->flag & SYS_ACL_ENTRY_FLAG_TUNNEL_INFO))) ||
            ((key_field->type == CTC_FIELD_KEY_TARGET_MAC) && !arp_target_mac))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "  DsLtidSelectAd.gArpFsCtl.%s is 0\n", !arp_target_mac? "dMacSel": "sMacSel");
            return CTC_E_INVALID_PARAM;
        }
        break;
    case CTC_FIELD_KEY_LABEL_NUM:                /**<  MPLS Label Number. */
    case  CTC_FIELD_KEY_MPLS_LABEL0:              /**<  Label Field of the MPLS Label 0. */
    case  CTC_FIELD_KEY_MPLS_EXP0:                /**<  Exp Field of the MPLS Label 0.*/
    case  CTC_FIELD_KEY_MPLS_SBIT0:               /**<  S-bit Field of the MPLS Label 0.*/
    case  CTC_FIELD_KEY_MPLS_TTL0:                /**<  Ttl Field of the MPLS Label 0.*/
    case  CTC_FIELD_KEY_MPLS_LABEL1:              /**<  Label Field of the MPLS Label 1. */
    case  CTC_FIELD_KEY_MPLS_EXP1:                /**<  Exp Field of the MPLS Label 1.*/
    case  CTC_FIELD_KEY_MPLS_SBIT1:               /**<  S-bit Field of the MPLS Label 1.*/
    case  CTC_FIELD_KEY_MPLS_TTL1:                /**<  Ttl Field of the MPLS Label 1.*/
    case  CTC_FIELD_KEY_MPLS_LABEL2:              /**<  Label Field of the MPLS Label 2. */
    case CTC_FIELD_KEY_MPLS_EXP2:                /**<  Exp Field of the MPLS Label 2.*/
    case CTC_FIELD_KEY_MPLS_SBIT2:               /**<  S-bit Field of the MPLS Label 2.*/
    case CTC_FIELD_KEY_MPLS_TTL2:                /**<  Ttl Field of the MPLS Label 2.*/
    case CTC_FIELD_KEY_MPLS_CW:
    case CTC_FIELD_KEY_MPLS_FWD_LABEL:
    case CTC_FIELD_KEY_MPLS_FWD_EXP:
    case CTC_FIELD_KEY_MPLS_FWD_SBIT:
    case CTC_FIELD_KEY_MPLS_FWD_TTL:
        if (CTC_PARSER_L3_TYPE_MPLS != l3_type && CTC_PARSER_L3_TYPE_MPLS_MCAST != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not MPLS l3 type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;
    case CTC_FIELD_KEY_NPM_IM_L_FLAG:
    case CTC_FIELD_KEY_NPM_IM_D_FLAG:
        CTC_MAX_VALUE_CHECK(key_field->data, 1);
    case CTC_FIELD_KEY_NPM_IM_FLOW_ID:
        if(DRV_FROM_AT(lchip))
        {
            return CTC_E_NONE;
        }
        if (CTC_PARSER_L3_TYPE_MPLS != l3_type && CTC_PARSER_L3_TYPE_MPLS_MCAST != l3_type &&
            CTC_PARSER_L3_TYPE_IPV4 != l3_type && CTC_PARSER_L3_TYPE_IPV6 != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not MPLS l3 type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }

        break;

    case CTC_FIELD_KEY_INTERFACE_ID:
        CTC_MAX_VALUE_CHECK(key_field->data, (MCHIP_CAP(SYS_CAP_SPEC_L3IF_NUM)-1));/* reserved*/
        break;

        /*Ether OAM packet field*/
    case CTC_FIELD_KEY_IS_Y1731_OAM:             /**<  Is Y1731 Oam. */
        if(CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHY1731 != l4_user_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Y1731 l4 user type must be ach oam\n");
            return CTC_E_INVALID_PARAM;
        }
        break;


    case CTC_FIELD_KEY_ETHER_OAM_LEVEL:          /**<  Oam Level. */
    case CTC_FIELD_KEY_ETHER_OAM_OP_CODE:        /**<  Oam Opcode. */
    case CTC_FIELD_KEY_ETHER_OAM_VERSION:        /**<  Oam Version. */
        if (!(CTC_PARSER_L3_TYPE_ETHER_OAM == l3_type
            || CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_Y1731)))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Param is mismatch: l3_type:%d l4_type:%d,l4_user_type:%d\n", l3_type, l4_type, l4_user_type);
            return CTC_E_INVALID_PARAM;
        }

        break;

        /*Slow Protocol packet field*/
    case CTC_FIELD_KEY_SLOW_PROTOCOL_CODE:       /**<  Slow Protocol Code. */
    case CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS:      /**<  Slow Protocol Flags. */
    case CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE:   /**<  Slow Protocol Sub Type. */
        if (CTC_PARSER_L3_TYPE_SLOW_PROTO != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not slow protocol l3_type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_PTP_MESSAGE_TYPE:       /**<  Slow Protocol Code. */
    case CTC_FIELD_KEY_PTP_VERSION:      /**<  Slow Protocol Flags. */
        if (CTC_PARSER_L3_TYPE_PTP != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not PTP  l3_type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_FCOE_DST_FCID:       /**<  Slow Protocol Code. */
    case CTC_FIELD_KEY_FCOE_SRC_FCID:       /**<  Slow Protocol Flags. */
        if (CTC_PARSER_L3_TYPE_FCOE != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not FCOE l3_type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_INGRESS_NICKNAME:         /**<  Ingress Nick Name. */
    case CTC_FIELD_KEY_EGRESS_NICKNAME:          /**<  Egress Nick Name. */
    case CTC_FIELD_KEY_IS_ESADI:                 /**<  Is Esadi. */
    case CTC_FIELD_KEY_IS_TRILL_CHANNEL:         /**<  Is Trill Channel. */
    case CTC_FIELD_KEY_TRILL_INNER_VLANID:       /**<  Trill Inner Vlan Id. */
    case CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID: /**<  Trill Inner Vlan Id Valid. */
    case CTC_FIELD_KEY_TRILL_LENGTH:             /**<  Trill Length. */
    case CTC_FIELD_KEY_TRILL_MULTIHOP:           /**<  Trill Multi-Hop. */
    case CTC_FIELD_KEY_TRILL_MULTICAST:          /**<  Trill MultiCast. */
    case CTC_FIELD_KEY_TRILL_VERSION:            /**<  Trill Version. */
    case CTC_FIELD_KEY_TRILL_TTL:                /**<  Trill Ttl. */
        if (CTC_PARSER_L3_TYPE_TRILL != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not TRILL l3_type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT:        /**<  Unknow 802.1AE packet, SecTag error or Mode not support. */
    case CTC_FIELD_KEY_DOT1AE_ES:                /**<  End Station (ES) bit. */
    case CTC_FIELD_KEY_DOT1AE_SC:                /**<  Secure Channel (SC) bit. */
    case CTC_FIELD_KEY_DOT1AE_AN:                /**<  Association Number (AN). */
    case CTC_FIELD_KEY_DOT1AE_SL:                /**<  Short Length (SL). */
    case CTC_FIELD_KEY_DOT1AE_PN:                /**<  Packet Number. */
    case CTC_FIELD_KEY_DOT1AE_SCI:               /**<  Secure Channel Identifier. */
    case CTC_FIELD_KEY_DOT1AE_CBIT :            /**< [TM] Changed Text. */
    case CTC_FIELD_KEY_DOT1AE_EBIT :            /**< [TM] Encryption. */
    case CTC_FIELD_KEY_DOT1AE_SCB :             /**< [TM] Single Copy Broadcast. */
    case CTC_FIELD_KEY_DOT1AE_VER :             /**< [TM] Version number. */
        if (CTC_PARSER_L3_TYPE_DOT1AE != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not DOT1AE l3_type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

        /*SATPDU packet field*/
    case  CTC_FIELD_KEY_SATPDU_MEF_OUI:           /**<  Satpdu Metro Ethernet Forum Oui.*/
    case CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE:      /**<  Satpdu Oui Sub type.*/
    case  CTC_FIELD_KEY_SATPDU_PDU_BYTE:          /**<  Satpdu Byte.*/
        if (CTC_PARSER_L3_TYPE_SATPDU != l3_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "L3 type is not SATPDU l3_type:%d\n", l3_type);
            return CTC_E_INVALID_PARAM;
        }

        break;

        /*Wlan packet field*/
    case CTC_FIELD_KEY_WLAN_RADIO_MAC:         /**<  Wlan Radio Mac.*/
    case CTC_FIELD_KEY_WLAN_RADIO_ID:          /**<  Wlan Radio Id.*/
    case  CTC_FIELD_KEY_WLAN_CTL_PKT:           /**<  Wlan Control Packet.*/
        if (CTC_PARSER_L4_TYPE_UDP != l4_type
            && CTC_PARSER_L4_USER_TYPE_UDP_CAPWAP != l4_user_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "WLAN param is mismatch l3_type:%d l4_user_type:%d\n", l4_type, l4_user_type);
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_AWARE_TUNNEL_INFO:
        if (CTC_PARSER_L4_TYPE_UDP == l4_type
            && CTC_PARSER_L4_USER_TYPE_UDP_VXLAN == l4_user_type)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "The filed must be decapsulated packet, l4_type:%d,l4_user_type:%d\n", l4_type, l4_user_type);
            return CTC_E_INVALID_PARAM;
        }
        if (CTC_PARSER_L4_TYPE_GRE == l4_type )
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "The filed must be decapsulated packet, l4_type:%d,l4_user_type:%d\n", l4_type, l4_user_type);
            return CTC_E_INVALID_PARAM;
        }
        if ((merge_data_mode & 7) == 0)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "The field is not support, merge_data_mode:%u\n", merge_data_mode);
            return CTC_E_INVALID_PARAM;
        }
        if(pe->flag & SYS_ACL_ENTRY_FLAG_SMAC)
        {
            return CTC_E_IN_USE;
        }
        break;

    case CTC_FIELD_KEY_CPU_REASON_ID:            /**<  CPU Reason Id. */
        if (NULL == key_field->ext_data)
        {
            CTC_MAX_VALUE_CHECK(key_field->data, CTC_PKT_CPU_REASON_MAX_COUNT);
        }
        else
        {
            CTC_MAX_VALUE_CHECK(key_field->data, SYS_USW_ACL_MATCH_GRP_MAX_COUNT);
        }
        break;
    case CTC_FIELD_KEY_DST_GPORT:                /**<  Fwd Destination Port. */
        SYS_GLOBAL_PORT_CHECK(key_field->data);
        break;
    case CTC_FIELD_KEY_DST_NHID:                 /**<  Nexthop Id. */
        if (key_field->ext_data && (0 == key_field->data) && (0 == key_field->mask))
        {
            uint32 dsnh_num = 0;
            sys_usw_ftm_query_table_entry_num(lchip, DsNextHop4W_t, &dsnh_num);
            CTC_MAX_VALUE_CHECK(*(uint32*)key_field->ext_data, dsnh_num-1);
        }
        break;
    case CTC_FIELD_KEY_STP_STATE:
        CTC_MAX_VALUE_CHECK(key_field->data, 3);
        break;
    case CTC_FIELD_KEY_SRV6_SRH_FLAG:
        CTC_MAX_VALUE_CHECK(key_field->data, 0xFF);
        break;
    case CTC_FIELD_KEY_VLAN_XLATE_HIT:
        break;
    case CTC_FIELD_KEY_CLASS_ID:
        SYS_USW_CLASS_ID_RANGE_CHECK(key_field->data);
        break;
    case CTC_FIELD_KEY_ETHER_TYPE:
        CTC_MIN_VALUE_CHECK(key_field->data, 0x600);
        break;
    case CTC_FIELD_KEY_BFD_MY_DISC:
    case CTC_FIELD_KEY_BFD_YOUR_DISC:
    	if(!((CTC_PARSER_L4_TYPE_UDP == l4_type && CTC_PARSER_L4_USER_TYPE_UDP_BFD == l4_user_type)||
			(CTC_PARSER_L4_TYPE_ACH_OAM == l4_type && ((CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHBFD == l4_user_type)||
			(CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHCC == l4_user_type)||
			(CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHCV == l4_user_type)))))
		{
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "BFD_MY_DISC and BFD_YOUR_DISC not support with , l4_type:%d \n", l4_type);
            return CTC_E_INVALID_PARAM;
		}
		break;
    default:
        break;
    }

    /*check ext_data valid*/
    switch(key_field->type)
    {
    case CTC_FIELD_KEY_MAC_SA:                  /* 48bit*/
    case CTC_FIELD_KEY_MAC_DA:
    case CTC_FIELD_KEY_SENDER_MAC:
    case CTC_FIELD_KEY_TARGET_MAC:
    case CTC_FIELD_KEY_WLAN_RADIO_MAC:
    case CTC_FIELD_KEY_IPV6_SA:                  /**<  Source IPv6 Address. 128bit */
    case CTC_FIELD_KEY_IPV6_DA:                  /**<  Destination IPv6 Address. 128bit */
    case CTC_FIELD_KEY_SATPDU_PDU_BYTE:    /*64 bit*/
    case CTC_FIELD_KEY_DOT1AE_SCI:         /*64 bit*/
    case CTC_FIELD_KEY_UDF:
    case CTC_FIELD_KEY_UDF_AD_ID:
    case CTC_FIELD_KEY_SUDF:
    case CTC_FIELD_KEY_SUDF_AD_ID:
    case CTC_FIELD_KEY_AWARE_TUNNEL_INFO:
    case CTC_FIELD_KEY_XDATA:
        CTC_PTR_VALID_CHECK(key_field->ext_data);
    break;
    case CTC_FIELD_KEY_SVLAN_RANGE:  /**<  Svlan Range; data: min, mask: max, ext_data: (uint32*)vrange_gid. */
    case CTC_FIELD_KEY_CVLAN_RANGE:  /**<  Cvlan Range; data: min, mask: max, ext_data: (uint32*)vrange_gid. */
        CTC_PTR_VALID_CHECK(key_field->ext_data);
        CTC_MAX_VALUE_CHECK(key_field->data, key_field->mask);
        SYS_ACL_VLAN_ID_CHECK(key_field->mask);
    break;
    case CTC_FIELD_KEY_PORT:
        CTC_PTR_VALID_CHECK(key_field->ext_data);
        CTC_ERROR_RETURN(_sys_usw_acl_check_port_info(lchip, pe, key_field));
    break;
    case CTC_FIELD_KEY_DISCARD:
        if ( key_field->ext_data)
        {
            uint32 discard_type = 0;
            discard_type = *((uint32*)(key_field->ext_data));
            CTC_ERROR_RETURN(_sys_usw_acl_check_discard_type(discard_type));
        }
    	break;
    }
    return CTC_E_NONE;

}

/* check union field for show/remove */
STATIC int32
_sys_tmm_acl_check_field_union2(uint8 lchip, uint32 qid, sys_acl_entry_t* pe)
{
    uint32 cmd = 0;
    ds_t  ds;
    uint8 l3_type = 0;
    uint8 l4_type = 0;
    uint8 l4_user_type = 0;
    uint8 gre_valid = 0, gre_key_valid = 0;
    uint8 arp_target_mac = 0, arp_sender_mac = 0;
    uint8 xgpon_use_gem_port = 0;
    uint8 merge_data_mode = 0;   /*bit0-vxlan vni valid, 1-gre no key valid, 2-gre key valid, 3-use mac da*/
    uint8 dir = pe->group->group_info.dir;
    uint32 tbl_id = 0;
    sys_acl_flex_key_t* fk = pe->ua->buffer.fk;

    tbl_id = (CTC_INGRESS == pe->group->group_info.dir ? DsLtidSelectAd0_t : DsEgrLtidSelectAd0_t);

    if(CTC_FIELD_KEY_GRE_KEY == qid || CTC_FIELD_KEY_NVGRE_KEY == qid || CTC_FIELD_KEY_GRE_PROTOCOL_TYPE == qid || CTC_FIELD_KEY_GRE_FLAGS == qid )
    {
        cmd = DRV_IOR(tbl_id+pe->group->group_info.block_id, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, fk->ad_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
        gre_valid = GetDsLtidSelectAd(V, fs16L4ByteGreShare_f, ds);
        gre_key_valid = GetDsLtidSelectAd(V, fs16L4ByteGreWithKeyShare_f, ds);
    }
    if(CTC_FIELD_KEY_MAC_SA == qid || CTC_FIELD_KEY_SENDER_MAC == qid)
    {
        cmd = DRV_IOR(tbl_id+pe->group->group_info.block_id, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, fk->ad_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
        arp_sender_mac = GetDsLtidSelectAd(V, gArpFsCtl_sMacSel_f, ds);
    }
    if(CTC_FIELD_KEY_MAC_DA == qid || CTC_FIELD_KEY_TARGET_MAC == qid)
    {
        cmd = DRV_IOR(tbl_id+pe->group->group_info.block_id, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, fk->ad_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
        arp_target_mac = GetDsLtidSelectAd(V, gArpFsCtl_dMacSel_f, ds);
    }
    l3_type = pe->l3_type;
    l4_type = pe->l4_type;
    l4_user_type = pe->l4_user_type;

    switch(qid)
    {
    case CTC_FIELD_KEY_GEM_PORT:
        if (dir == CTC_INGRESS)
        {
            cmd = DRV_IOR(IpeAclQosCtl_t, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
            xgpon_use_gem_port = GetIpeAclQosCtl(V, useGemPortLookup_f, ds);
        }
        if (!xgpon_use_gem_port)
        {
            return CTC_E_NOT_SUPPORT;
        }
        break;

    case CTC_FIELD_KEY_MAC_SA:                   /**<  Source MAC Address. */
        if(pe->flag & SYS_ACL_ENTRY_FLAG_TUNNEL_INFO)
        {
            return CTC_E_INVALID_PARAM;
        }
    case CTC_FIELD_KEY_MAC_DA:                   /**<  Destination MAC Address. */
        if (((qid == CTC_FIELD_KEY_MAC_SA) && arp_sender_mac && l3_type == CTC_PARSER_L3_TYPE_ARP) ||
            ((qid == CTC_FIELD_KEY_MAC_DA) && arp_target_mac && l3_type == CTC_PARSER_L3_TYPE_ARP))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_IP_SA:                    /**<  Source IPv4 Address. */
    case CTC_FIELD_KEY_IP_DA:
        if (l3_type != CTC_PARSER_L3_TYPE_IPV4)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_IPV6_SA:                  /**<  Source IPv6 Address. */
    case CTC_FIELD_KEY_IPV6_DA:                  /**<  Destination IPv6 Address. */
    case CTC_FIELD_KEY_IPV6_FLOW_LABEL:          /**<  Ipv6 Flow label*/
        if (l3_type != CTC_PARSER_L3_TYPE_IPV6)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_IP_TTL:
    case CTC_FIELD_KEY_IP_DSCP:                  /**<  DSCP. */
    case CTC_FIELD_KEY_IP_PKT_LEN_RANGE:
    case CTC_FIELD_KEY_IP_PROTOCOL:              /**<  Ip Protocol. */
    case CTC_FIELD_KEY_IP_PRECEDENCE:            /**<  Precedence. */
    case CTC_FIELD_KEY_IP_ECN:                   /**<  Ecn. */
    case CTC_FIELD_KEY_IP_FRAG:                  /**<  IP Fragment Information. */
    case CTC_FIELD_KEY_IP_HDR_ERROR:             /**<  Ip Header Error. */
    case CTC_FIELD_KEY_IP_OPTIONS:               /**<  Ip Options. */
        if (l3_type != CTC_PARSER_L3_TYPE_IPV4
            && l3_type != CTC_PARSER_L3_TYPE_IPV6)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_L4_DST_PORT:
    case CTC_FIELD_KEY_L4_SRC_PORT:
        if (CTC_PARSER_L3_TYPE_IPV4 != l3_type
            && CTC_PARSER_L3_TYPE_IPV6 != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        if (l4_type == CTC_PARSER_L4_TYPE_NONE
            || CTC_PARSER_L4_TYPE_ICMP == l4_type
        || CTC_PARSER_L4_TYPE_IGMP == l4_type)
        {
            return CTC_E_PARAM_CONFLICT;
        }
        /*l4ByteVxlanShare = (l3IsIpv4 || l3IsIpv6) && vxlanShareValid && TempDsLtidSelectAd.fs16L4ByteVxlanShare;*/
        if(l4_type == CTC_PARSER_L4_TYPE_UDP && l4_user_type == CTC_PARSER_L4_USER_TYPE_UDP_VXLAN)
        {
            cmd = DRV_IOR(tbl_id+pe->group->group_info.block_id, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, fk->ad_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
            if(GetDsLtidSelectAd(V, fs16L4ByteVxlanShare_f, ds))
            {
                return CTC_E_PARAM_CONFLICT;
            }
        }
        break;

    case CTC_FIELD_KEY_L4_SRC_PORT_RANGE:        /**<  Layer 4 Src Port Range; data: min, mask: max. */
    case CTC_FIELD_KEY_L4_DST_PORT_RANGE:        /**<  Layer 4 Dest Port Range;  data: min, mask: max. */
        if (CTC_PARSER_L3_TYPE_IPV4 != l3_type
            && CTC_PARSER_L3_TYPE_IPV6 != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        if (l4_type == CTC_PARSER_L4_TYPE_NONE
            || CTC_PARSER_L4_TYPE_ICMP == l4_type
        || CTC_PARSER_L4_TYPE_IGMP == l4_type)
        {
            return CTC_E_PARAM_CONFLICT;
        }
        break;

    case CTC_FIELD_KEY_TCP_ECN:                  /**<  TCP Ecn. */
    case CTC_FIELD_KEY_TCP_FLAGS:                /**<  TCP Flags (ctc_acl_tcp_flag_flag_t). */
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4  && l3_type != CTC_PARSER_L3_TYPE_IPV6 )
            || (l4_type != CTC_PARSER_L4_TYPE_TCP))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_ICMP_CODE:
    case CTC_FIELD_KEY_ICMP_TYPE:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_ICMP))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_IGMP_TYPE:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_IGMP))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_VXLAN_FLAGS:
    case CTC_FIELD_KEY_VXLAN_RSV1:
    case CTC_FIELD_KEY_VXLAN_RSV2:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_UDP)
            || (l4_user_type != CTC_PARSER_L4_USER_TYPE_UDP_VXLAN))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_VN_ID:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_UDP)
        || (l4_user_type != CTC_PARSER_L4_USER_TYPE_UDP_VXLAN))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_GRE_KEY:                     /**< Gre Key. */
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_GRE))
        {
            return CTC_E_INVALID_PARAM;
        }
        if (!gre_valid || !gre_key_valid)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_NVGRE_KEY:                  /**<  NVGre Key. */
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_GRE))
        {
            return CTC_E_INVALID_PARAM;
        }
        if (!gre_valid || !gre_key_valid)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_GRE_PROTOCOL_TYPE:
    case CTC_FIELD_KEY_GRE_FLAGS:
        if ((l3_type != CTC_PARSER_L3_TYPE_IPV4 && l3_type != CTC_PARSER_L3_TYPE_IPV6)
            || (l4_type != CTC_PARSER_L4_TYPE_GRE))
        {
            return CTC_E_INVALID_PARAM;
        }
        if (!gre_valid || gre_key_valid)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_ARP_OP_CODE:
    case CTC_FIELD_KEY_ARP_PROTOCOL_TYPE:
    case CTC_FIELD_KEY_ARP_SENDER_IP:
    case CTC_FIELD_KEY_ARP_TARGET_IP:
    case CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL:
    case CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL:
    case CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL:
    case CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL:
    case CTC_FIELD_KEY_SENDER_MAC:                   /**<  Ethernet Address of sender of ARP Header */
    case CTC_FIELD_KEY_TARGET_MAC:                   /**<  Ethernet Address of destination of ARP Header */
    case CTC_FIELD_KEY_GARP:
    case CTC_FIELD_KEY_RARP:
        if (CTC_PARSER_L3_TYPE_ARP != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        if (((qid == CTC_FIELD_KEY_SENDER_MAC) && (!arp_sender_mac || (pe->flag & SYS_ACL_ENTRY_FLAG_TUNNEL_INFO))) ||
            ((qid == CTC_FIELD_KEY_TARGET_MAC) && !arp_target_mac))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_LABEL_NUM:                /**<  MPLS Label Number. */
    case CTC_FIELD_KEY_MPLS_LABEL0:              /**<  Label Field of the MPLS Label 0. */
    case CTC_FIELD_KEY_MPLS_EXP0:                /**<  Exp Field of the MPLS Label 0.*/
    case CTC_FIELD_KEY_MPLS_SBIT0:               /**<  S-bit Field of the MPLS Label 0.*/
    case CTC_FIELD_KEY_MPLS_TTL0:                /**<  Ttl Field of the MPLS Label 0.*/
    case CTC_FIELD_KEY_MPLS_LABEL1:              /**<  Label Field of the MPLS Label 1. */
    case CTC_FIELD_KEY_MPLS_EXP1:                /**<  Exp Field of the MPLS Label 1.*/
    case CTC_FIELD_KEY_MPLS_SBIT1:               /**<  S-bit Field of the MPLS Label 1.*/
    case CTC_FIELD_KEY_MPLS_TTL1:                /**<  Ttl Field of the MPLS Label 1.*/
    case CTC_FIELD_KEY_MPLS_LABEL2:              /**<  Label Field of the MPLS Label 2. */
    case CTC_FIELD_KEY_MPLS_EXP2:                /**<  Exp Field of the MPLS Label 2.*/
    case CTC_FIELD_KEY_MPLS_SBIT2:               /**<  S-bit Field of the MPLS Label 2.*/
    case CTC_FIELD_KEY_MPLS_TTL2:                /**<  Ttl Field of the MPLS Label 2.*/
    case CTC_FIELD_KEY_MPLS_CW:
    case CTC_FIELD_KEY_MPLS_FWD_LABEL:
    case CTC_FIELD_KEY_MPLS_FWD_EXP:
    case CTC_FIELD_KEY_MPLS_FWD_SBIT:
    case CTC_FIELD_KEY_MPLS_FWD_TTL:
        if (CTC_PARSER_L3_TYPE_MPLS != l3_type && CTC_PARSER_L3_TYPE_MPLS_MCAST != l3_type && CTC_PARSER_L3_TYPE_NONE != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;
    case CTC_FIELD_KEY_NPM_IM_FLOW_ID:
    case CTC_FIELD_KEY_NPM_IM_L_FLAG:
    case CTC_FIELD_KEY_NPM_IM_D_FLAG:
        if (CTC_PARSER_L3_TYPE_MPLS != l3_type && CTC_PARSER_L3_TYPE_MPLS_MCAST != l3_type && CTC_PARSER_L3_TYPE_NONE != l3_type &&
            CTC_PARSER_L3_TYPE_IPV4 != l3_type && CTC_PARSER_L3_TYPE_IPV6 != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }

        break;

    case CTC_FIELD_KEY_INTERFACE_ID:
        break;

        /*Ether OAM packet field*/
    case CTC_FIELD_KEY_IS_Y1731_OAM:             /**<  Is Y1731 Oam. */
        if(CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHY1731 != l4_user_type && CTC_PARSER_L4_USER_TYPE_NONE != l4_user_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_ETHER_OAM_LEVEL:          /**<  Oam Level. */
    case CTC_FIELD_KEY_ETHER_OAM_OP_CODE:        /**<  Oam Opcode. */
    case CTC_FIELD_KEY_ETHER_OAM_VERSION:        /**<  Oam Version. */
        if (!(CTC_PARSER_L3_TYPE_ETHER_OAM == l3_type || CTC_PARSER_L3_TYPE_NONE == l3_type
            || CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_Y1731)))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

        /*Slow Protocol packet field*/
    case CTC_FIELD_KEY_SLOW_PROTOCOL_CODE:       /**<  Slow Protocol Code. */
    case CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS:      /**<  Slow Protocol Flags. */
    case CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE:   /**<  Slow Protocol Sub Type. */
        if (CTC_PARSER_L3_TYPE_SLOW_PROTO != l3_type && CTC_PARSER_L3_TYPE_NONE != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_PTP_MESSAGE_TYPE:       /**<  Slow Protocol Code. */
    case CTC_FIELD_KEY_PTP_VERSION:      /**<  Slow Protocol Flags. */
        if (CTC_PARSER_L3_TYPE_PTP != l3_type && CTC_PARSER_L3_TYPE_NONE != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_FCOE_DST_FCID:       /**<  Slow Protocol Code. */
    case CTC_FIELD_KEY_FCOE_SRC_FCID:       /**<  Slow Protocol Flags. */
        if (CTC_PARSER_L3_TYPE_FCOE != l3_type && CTC_PARSER_L3_TYPE_NONE != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_INGRESS_NICKNAME:         /**<  Ingress Nick Name. */
    case CTC_FIELD_KEY_EGRESS_NICKNAME:          /**<  Egress Nick Name. */
    case CTC_FIELD_KEY_IS_ESADI:                 /**<  Is Esadi. */
    case CTC_FIELD_KEY_IS_TRILL_CHANNEL:         /**<  Is Trill Channel. */
    case CTC_FIELD_KEY_TRILL_INNER_VLANID:       /**<  Trill Inner Vlan Id. */
    case CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID: /**<  Trill Inner Vlan Id Valid. */
    case CTC_FIELD_KEY_TRILL_LENGTH:             /**<  Trill Length. */
    case CTC_FIELD_KEY_TRILL_MULTIHOP:           /**<  Trill Multi-Hop. */
    case CTC_FIELD_KEY_TRILL_MULTICAST:          /**<  Trill MultiCast. */
    case CTC_FIELD_KEY_TRILL_VERSION:            /**<  Trill Version. */
    case CTC_FIELD_KEY_TRILL_TTL:                /**<  Trill Ttl. */
        if (CTC_PARSER_L3_TYPE_TRILL != l3_type && CTC_PARSER_L3_TYPE_NONE != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

    case CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT:        /**<  Unknow 802.1AE packet, SecTag error or Mode not support. */
    case CTC_FIELD_KEY_DOT1AE_ES:                /**<  End Station (ES) bit. */
    case CTC_FIELD_KEY_DOT1AE_SC:                /**<  Secure Channel (SC) bit. */
    case CTC_FIELD_KEY_DOT1AE_AN:                /**<  Association Number (AN). */
    case CTC_FIELD_KEY_DOT1AE_SL:                /**<  Short Length (SL). */
    case CTC_FIELD_KEY_DOT1AE_PN:                /**<  Packet Number. */
    case CTC_FIELD_KEY_DOT1AE_SCI:               /**<  Secure Channel Identifier. */
    case CTC_FIELD_KEY_DOT1AE_CBIT :            /**< [TM] Changed Text. */
    case CTC_FIELD_KEY_DOT1AE_EBIT :            /**< [TM] Encryption. */
    case CTC_FIELD_KEY_DOT1AE_SCB :             /**< [TM] Single Copy Broadcast. */
    case CTC_FIELD_KEY_DOT1AE_VER :             /**< [TM] Version number. */
        if (CTC_PARSER_L3_TYPE_DOT1AE != l3_type && CTC_PARSER_L3_TYPE_NONE != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        break;

        /*SATPDU packet field*/
    case CTC_FIELD_KEY_SATPDU_MEF_OUI:           /**<  Satpdu Metro Ethernet Forum Oui.*/
    case CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE:      /**<  Satpdu Oui Sub type.*/
    case CTC_FIELD_KEY_SATPDU_PDU_BYTE:          /**<  Satpdu Byte.*/
        if (CTC_PARSER_L3_TYPE_SATPDU != l3_type && CTC_PARSER_L3_TYPE_NONE != l3_type)
        {
            return CTC_E_INVALID_PARAM;
        }

        break;


    case CTC_FIELD_KEY_AWARE_TUNNEL_INFO:
        if (CTC_PARSER_L4_TYPE_UDP == l4_type
            && CTC_PARSER_L4_USER_TYPE_UDP_VXLAN == l4_user_type)
        {
            return CTC_E_INVALID_PARAM;
        }
        if (CTC_PARSER_L4_TYPE_GRE == l4_type )
        {
            return CTC_E_INVALID_PARAM;
        }
        if (dir == CTC_INGRESS)
        {
            cmd = DRV_IOR(ProgramTcamAclCtl_t, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
            merge_data_mode = GetProgramTcamAclCtl(V, fsMergeDataShareTypeCtl_f, ds);
        }
        else
        {
            cmd = DRV_IOR(EgrProgramTcamAclCtl_t, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), ds));
            merge_data_mode = GetProgramTcamAclCtl(V, fsMergeDataShareTypeCtl_f, ds);
        }
        if ((merge_data_mode & 7) == 0 || (pe->flag & SYS_ACL_ENTRY_FLAG_SMAC))
        {
            return CTC_E_INVALID_PARAM;
        }
        break;
    case CTC_FIELD_KEY_BFD_MY_DISC:
    case CTC_FIELD_KEY_BFD_YOUR_DISC:
    	if(!((CTC_PARSER_L4_TYPE_UDP == l4_type && CTC_PARSER_L4_USER_TYPE_UDP_BFD == l4_user_type)||
			(CTC_PARSER_L4_TYPE_ACH_OAM == l4_type && ((CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHBFD == l4_user_type)||
			(CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHCC == l4_user_type)||
			(CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHCV == l4_user_type)))))
		{
			    
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "BFD_MY_DISC and BFD_Your_DISC need l4_user_type with, l4_type:%d \n", l4_type);
            return CTC_E_INVALID_PARAM;
		}
		break;
    default:
        break;
    }

    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_set_field_union(uint8 lchip, uint32 qid, ctc_field_key_t* key_field, sys_acl_entry_t* pe, uint8 is_add)
{
    switch(qid)
    {
        case CTC_FIELD_KEY_L2_TYPE:
            pe->l2_type = is_add? key_field->data: 0;
            break;
        case CTC_FIELD_KEY_L3_TYPE:
            pe->l3_type = is_add? key_field->data: 0;
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_L3_TYPE, is_add);
            break;
        case CTC_FIELD_KEY_L4_TYPE:
            pe->l4_type = is_add? key_field->data: 0;
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_L4_TYPE, is_add);
            break;
        case CTC_FIELD_KEY_L4_USER_TYPE:
            pe->l4_user_type = is_add? key_field->data: 0;
            break;
        case CTC_FIELD_KEY_DST_NHID:
            //pe->un.normal.key_dst_nhid = key_field->data;
            //SYS_ACL_SET_AD_TYPE(pe, !pe->ad_type, SYS_ACL_AD_TYPE_PERMIT, is_add);
            break;
        case CTC_FIELD_KEY_CPU_REASON_ID:
            //pe->key_reason_id = key_field->data;
            break;
        case CTC_FIELD_KEY_IS_Y1731_OAM:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_Y1731, is_add);
            break;
        case CTC_FIELD_KEY_CLASS_ID:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_CLASS_ID, is_add);
            break;
        case CTC_FIELD_KEY_UDF:     /*0:udf_id  1:udf_ad_id;  for show*/
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_UDF, 0);
            break;
        case CTC_FIELD_KEY_UDF_AD_ID:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_UDF, is_add);
            break;
        case CTC_FIELD_KEY_SUDF:    /*0:sudf_id 1:sudf_ad_id; for show*/
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_SUDF, 0);
            break;
        case CTC_FIELD_KEY_SUDF_AD_ID:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_SUDF, is_add);
            break;
        case CTC_FIELD_KEY_NPM_IM_L_FLAG:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_NPM_IM_L_FLAG, is_add);
            break;
        case CTC_FIELD_KEY_NPM_IM_D_FLAG:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_NPM_IM_D_FLAG, is_add);
            break;
        case SYS_ACL_FIELD_PORT_BITMAP_MODE0:
        case SYS_ACL_FIELD_PORT_BITMAP_MODE1:
        case SYS_ACL_FIELD_PORT_BITMAP_MODE2:
        case SYS_ACL_FIELD_PORT_BITMAP_MODE3:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_SYS_BMP_MODEX, is_add);
            break;
        case CTC_FIELD_KEY_LOGIC_SRC_PORT:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_LOGIC_PORT, is_add);
            break;
        case CTC_FIELD_KEY_PORT:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_GPORT, is_add);
            break;
        case SYS_ACL_FIELD_GPORT_SHARE:
            pe->key_port_mode = is_add ? SYS_ACL_KEY_SHARE_MODE_PORT : 0;
            break;
        case SYS_ACL_FIELD_LOGIC_PORT_SHARE:
            pe->key_port_mode = is_add ? SYS_ACL_KEY_SHARE_MODE_LOGIC_PORT : 0;
            break;
        case CTC_FIELD_KEY_METADATA:
            pe->key_port_mode = is_add ? SYS_ACL_KEY_SHARE_MODE_METADATA : 0;
            break;
        case SYS_ACL_FIELD_PBM:
            pe->key_port_mode = is_add ? SYS_ACL_KEY_SHARE_MODE_PBM : 0;
            break;
        case CTC_FIELD_KEY_MAC_SA:
        case CTC_FIELD_KEY_SENDER_MAC:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_SMAC, is_add);
            break;
        case CTC_FIELD_KEY_AWARE_TUNNEL_INFO:
            SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_TUNNEL_INFO, is_add);
            break;
    }
    return CTC_E_NONE;
}

STATIC uint8
sys_tmm_check_srv6_field_is_used(uint8 lchip, sys_acl_entry_t* pe)
{
    uint16 qid = 0;
    uint16 offset_dst = 0;
    uint32 value_array = 0;
    uint32 mask_array = 0;
    uint32 key_width = 0;

    for (qid = CTC_FIELD_KEY_SRV6_SRH_FLAG; qid <= CTC_FIELD_KEY_SRV6_SRH_TAG ; qid++)
    {
        _sys_tmm_acl_get_key_field(lchip, qid, pe, &key_width, &value_array, &mask_array, &offset_dst);

        if(mask_array)
        {
            return TRUE; /*srv6 field is used*/
        }

    }

    return FALSE;
}

/* no need to use p_data and p_mask to remove field, build qid process mask by width */
int32
sys_tmm_acl_build_key_field(uint8 lchip, ctc_field_key_t* pKey, void* pe_info,uint8 is_add)
{
    uint8  discard_type = 0;
    uint8  loop = 0;
    uint32 qid = pKey->type;
    uint32 port_qid = 0;
    uint32 value = 0;
    uint32 mask = 0;
    int32  ret = CTC_E_NONE;
    uint8 tmp_l3_type = 0;
    hw_mac_addr_t hw_mac = {0};
    hw_mac_addr_t hw_mac_mask = {0};
    uint32 value_arr[4] = {0};
    uint32 mask_arr[4] = {0};
    void*  p_data = NULL;
    void*  p_mask = NULL;
    sys_acl_entry_t* pe = NULL;
    sys_acl_flex_key_t* fk = NULL;
    sys_npm_im_flow_hash_t npm_im_flow;
    ds_t ext_mask_temp;
    uint8 dir = 0;
    uint8 flow_type = 0;
    uint8 is_ipv6 = 0;
    uint8 global_pp_en = 0;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    sal_memset(&npm_im_flow, 0, sizeof(npm_im_flow));

    pe = (sys_acl_entry_t*) pe_info;
    if(is_add)
    {
        CTC_ERROR_RETURN(_sys_tmm_acl_check_field_union(lchip, pKey, pe));
        if (pKey->ext_mask == NULL)
        {
            sal_memset(ext_mask_temp, 0xFF, sizeof(ds_t));
            pKey->ext_mask = ext_mask_temp;
        }
    }
    else
    {
        CTC_ERROR_RETURN(_sys_tmm_acl_check_field_union2(lchip, qid, pe));
    }

    fk = pe->ua->buffer.fk;
    p_data = &value;
    p_mask = &mask;
    value = pKey->data ;
    mask =  pKey->mask ;
    dir = pe->group->group_info.dir;

    /*process data/mask*/
    switch(pKey->type)
    {
    case CTC_FIELD_KEY_MAC_DA:
    case CTC_FIELD_KEY_MAC_SA:
    case CTC_FIELD_KEY_SENDER_MAC:
    case CTC_FIELD_KEY_TARGET_MAC:
        {
            if (is_add)
            {
                SYS_SET_HW_MAC(hw_mac, (uint8*)(pKey->ext_data));
                SYS_SET_HW_MAC(hw_mac_mask, (uint8*)(pKey->ext_mask));
            }
            p_data = hw_mac;
            p_mask = hw_mac_mask;
        }
        break;

    case CTC_FIELD_KEY_SVLAN_RANGE:
    case CTC_FIELD_KEY_CVLAN_RANGE:
    case CTC_FIELD_KEY_IP_PKT_LEN_RANGE:
    case CTC_FIELD_KEY_L4_SRC_PORT_RANGE:
    case CTC_FIELD_KEY_L4_DST_PORT_RANGE:
    case CTC_FIELD_KEY_SUDF_RANGE:
    case CTC_FIELD_KEY_UDF_RANGE:
        {
            if (!CTC_ACL_KSET_ISSET(fk->kset, pKey->type))
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
                CTC_ERROR_RETURN(CTC_E_NOT_SUPPORT);
            }
            CTC_ERROR_RETURN(_sys_tmm_acl_get_range_bitmap(lchip, pe, pKey, is_add, p_data, p_mask));
            if (!is_add && mask!=0)
            {
                is_add = TRUE;
            }
        }
        break;

    case  CTC_FIELD_KEY_AWARE_TUNNEL_INFO:
    {
        if (is_add)
        {
            ctc_acl_tunnel_data_t* p_tunnel_data = NULL;
            ctc_acl_tunnel_data_t* p_tunnel_mask = NULL;

            p_tunnel_data = (ctc_acl_tunnel_data_t*)(pKey->ext_data);
            p_tunnel_mask = (ctc_acl_tunnel_data_t*)(pKey->ext_mask);

            if (p_tunnel_data ->type == 1)
            {
                value_arr[0] = p_tunnel_data ->vxlan_vni;
                value_arr[1] = 1<<30;
                value_arr[2] = 1;
                mask_arr[0] = p_tunnel_mask ->vxlan_vni & 0xFFFFFF;
                mask_arr[1] = 3<<30;
                mask_arr[2] = 1;
            }
            else if(p_tunnel_data ->type == 2)
            {
                value_arr[0] = p_tunnel_data ->gre_key;
                value_arr[1] = 3<<30;
                value_arr[2] = 1;
                mask_arr[0] = p_tunnel_mask ->gre_key;
                mask_arr[1] = 3<<30;
                mask_arr[2] = 1;
            }
            else
            {
                CTC_ERROR_RETURN(CTC_E_NOT_SUPPORT);
            }
        }

        p_data = value_arr;
        p_mask = mask_arr;
    }
        break;

    case CTC_FIELD_KEY_IP_PROTOCOL:
        if((pKey->mask & 0xFF) == 0xFF)
        {
            uint32 tmp_value;
            uint32 tmp_mask;
            sys_usw_acl_map_ip_protocol_to_l4_type
                                        (lchip, pe->l3_type, pKey->data, &tmp_value, &tmp_mask);
            if(tmp_value != CTC_PARSER_L4_TYPE_NONE)
            {
                pe->l4_type = tmp_value;
                if(SYS_ACL_KEY_IS_RSVD(pe->key_type) && CTC_ACL_KSET_ISSET(fk->kset, CTC_FIELD_KEY_L4_TYPE) )
                {
                    qid = CTC_FIELD_KEY_L4_TYPE;
                    value = is_add ? sys_usw_l4_type_map(lchip, tmp_value, TRUE) : 0;
                    mask = is_add ? tmp_mask : 0;
                    pe->l4_type = is_add? pe->l4_type : 0;
                }
            }
            else if(!is_add && !CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_L4_TYPE))
            {
                pe->l4_type = CTC_PARSER_L4_TYPE_NONE;
            }
        }
        break;

    case CTC_FIELD_KEY_L3_TYPE:
        if(is_add && pKey->data == CTC_PARSER_L3_TYPE_IP)
        {
            value = CTC_PARSER_L3_TYPE_IPV4;
            mask = 0x0e;
        }
        break;
    case CTC_FIELD_KEY_ETHER_TYPE :
        if (!CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_FULL_ETHER_TYPE) && !CTC_ACL_KSET_ISSET(fk->kset, CTC_FIELD_KEY_ETHER_TYPE))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
            CTC_ERROR_RETURN(CTC_E_NOT_SUPPORT);
        }
        _sys_usw_acl_map_ethertype(pKey->data, &tmp_l3_type);
        if (tmp_l3_type != CTC_PARSER_L3_TYPE_NONE)
        {
            pe->l3_type = tmp_l3_type;
            if(SYS_ACL_KEY_IS_RSVD(pe->key_type) && CTC_ACL_KSET_ISSET(fk->kset, CTC_FIELD_KEY_L3_TYPE))
            {
                qid = CTC_FIELD_KEY_L3_TYPE;
                value = is_add ? tmp_l3_type : 0;
                mask = is_add ? 0xF : 0;
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "Key filed:%s (EthType:0x%x) mapping to %s (l3_type:%d)n", "CTC_FIELD_KEY_ETHER_TYPE", pKey->data, \
                "CTC_FIELD_KEY_L3_TYPE", tmp_l3_type);
                pe->l3_type = is_add? value : 0;
                break;
            }
        }
        else if(!is_add && !CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_L3_TYPE))
        {
            pe->l3_type = CTC_PARSER_L3_TYPE_NONE;
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_FULL_ETHER_TYPE))
        {
            qid = SYS_ACL_FIELD_FULL_ETHER_TYPE;
            //pe->ether_type = is_add? pKey->data : 0;
            if((pKey->mask & 0xFFFF) == 0xFFFF && SYS_ACL_KEY_IS_RSVD(pe->key_type) && CTC_ACL_KSET_ISSET(fk->kset, CTC_FIELD_KEY_L3_TYPE))
            {
                uint8 tmp_l3_type = 0;
                _sys_usw_acl_map_ethertype(pKey->data, &tmp_l3_type);
                if (tmp_l3_type != CTC_PARSER_L3_TYPE_NONE)
                {
                    qid = CTC_FIELD_KEY_L3_TYPE;
                    value = is_add ? tmp_l3_type : 0;
                    mask = is_add ? 0xF : 0;
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "Key filed:%s (EthType:0x%x) mapping to %s (l3_type:%d)n", "CTC_FIELD_KEY_ETHER_TYPE", pKey->data, \
                    "CTC_FIELD_KEY_L3_TYPE", tmp_l3_type);
                    pe->l3_type = is_add? value : 0;
                }
            }
            break;
        }
        global_pp_en = SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, pe->group->group_info.dir, pe->group->group_info.block_id);
        if (is_add)
        {
            uint8 temp_ether_type_index = pe->un.g4.ether_type_index;
            uint16 temp_ether_type = 0;
            uint8 c_ether = 0;

            temp_ether_type = sys_usw_acl_get_ether_type_by_index(lchip, pe, temp_ether_type_index);

            CTC_ERROR_RETURN(sys_tmm_acl_add_compress_ether_type(lchip, global_pp_en, pe->group->group_info.dir, pKey->data, temp_ether_type, &c_ether, &temp_ether_type_index));
            value = (c_ether & 0x3F);
            mask = 0x3F;
            pe->un.g4.ether_type_index = temp_ether_type_index;
            pe->un.g4.ether_type_valid = 1;
        }
        else
        {
              sys_tmm_acl_remove_compress_ether_type(lchip, global_pp_en, pe->group->group_info.dir, pe->un.g4.ether_type_index);
              value = 0;
              mask = 0;
              pe->un.g4.ether_type_index = 0;
              pe->un.g4.ether_type_valid = 0;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "eth type:0x%x\n", pKey->data);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "ceth type:0x%x\n", value);
        break;
    case CTC_FIELD_KEY_PKT_FWD_TYPE:
        if (is_add)
        {
            uint8 fwd_type = 0;
            CTC_ERROR_RETURN(sys_usw_acl_map_fwd_type(pKey->data, &fwd_type));
            value = fwd_type;
            mask = pKey->mask;
        }
        break;
    case CTC_FIELD_KEY_DOT1AE_SCI:
    case CTC_FIELD_KEY_SATPDU_PDU_BYTE:
        if (is_add)
        {
            ACL_SET_HW_SATPDU_BYTE(value_arr, (uint32*)pKey->ext_data);
            ACL_SET_HW_SATPDU_BYTE(mask_arr, (uint32*)pKey->ext_mask);
        }
        p_data = value_arr;
        p_mask = mask_arr;
        break;
    case CTC_FIELD_KEY_DISCARD:
        if (is_add)
        {
            if (pKey->ext_data)
            {
                discard_type = *((uint32*)(pKey->ext_data));
            }
            value = (pKey->data << 6) | discard_type;
            mask = (pKey->ext_data) ? 0x3F : 0;
            mask |= (pKey->mask << 6);
        }
        break;
    case CTC_FIELD_KEY_METADATA:
/*spec: bit width keep 14*/
        SYS_ACL_CHECK_UNION_FLAG_CONFLICT(pe->key_port_mode, SYS_ACL_KEY_SHARE_MODE_METADATA);
        if (is_add)
        {
            value = (pKey->data & 0x3FFF) | (CTC_METADATA_TYPE_METADATA << 14) | (DRV_ENUM(DRV_FLOWPORTTYPE_METADATA) << 16);
            mask = (pKey->mask & 0x3FFF) | (0xF << 14);
        }
        break;
    case CTC_FIELD_KEY_L4_USER_TYPE:
        value = is_add? sys_usw_l4_user_type_map(lchip, pKey->data, TRUE) : 0;
        mask = is_add? pKey->mask: 0;
        break;
    case CTC_FIELD_KEY_NPM_IM_FLOW_ID:
    case CTC_FIELD_KEY_NPM_IM_L_FLAG:
    case CTC_FIELD_KEY_NPM_IM_D_FLAG:
        if(is_add && pKey->type == CTC_FIELD_KEY_NPM_IM_FLOW_ID)
        {
            CTC_ERROR_RETURN(SYS_CB_RET(CTC_E_NOT_SUPPORT, SYS_CB_NPM_IM_FLOW_TO_ACL, lchip, pKey->data, (void*)&npm_im_flow));
            flow_type = npm_im_flow.flow_type;
            is_ipv6 = ((SYS_IM_TYPE_IPV6 == npm_im_flow.ip_im_type) || (SYS_IM_TYPE_VXLAN_IPV6 == npm_im_flow.ip_im_type))?1:0;
            pe->im_d_flag = ((dir == CTC_EGRESS) && (flow_type == CTC_NPM_IM_FLOW_TYPE_SINK))?1:0;
        }
        else
        {
            uint32 key_width = 0;
            uint16 offset = 0;
            _sys_tmm_acl_get_key_field(lchip, CTC_FIELD_KEY_NPM_IM_FLOW_ID, pe, &key_width, value_arr, mask_arr, &offset);
            _sys_tmm_acl_get_key_field(lchip, CTC_FIELD_KEY_NPM_IM_L_FLAG, pe, &key_width, value_arr, mask_arr, &offset);
            _sys_tmm_acl_get_key_field(lchip, CTC_FIELD_KEY_NPM_IM_D_FLAG, pe, &key_width, value_arr, mask_arr, &offset);
            if(value_arr[0])
            {
                flow_type = 1; /* 1:Detect Node(Transmit or Sink) */
            }
            else
            {
                _sys_tmm_acl_get_key_field(lchip, SYS_ACL_FIELD_IPV6_IM_FLOW_ID, pe, &key_width, value_arr, mask_arr, &offset);
                _sys_tmm_acl_get_key_field(lchip, SYS_ACL_FIELD_IPV6_IM_L_FLAG, pe, &key_width, value_arr, mask_arr, &offset);
                _sys_tmm_acl_get_key_field(lchip, SYS_ACL_FIELD_IPV6_IM_D_FLAG, pe, &key_width, value_arr, mask_arr, &offset);
                if(value_arr[0])
                {
                    flow_type = 1; /* 1:Detect Node(Transmit or Sink) */
                    is_ipv6 =1;
                }
            }
        }
        if(DRV_FROM_AT(lchip))
        {
            if(pKey->type == CTC_FIELD_KEY_NPM_IM_L_FLAG)
            {
                value = (1 << 1) | pKey->data;
                mask  = (1 << 1) | pKey->mask;
            }
            if(dir == CTC_EGRESS && flow_type != 1)    /* Source and Sink Node, egress use xdata hit flowId */
            {
                if(pKey->type == CTC_FIELD_KEY_NPM_IM_FLOW_ID)
                {
                    sys_usw_npm_im_convert_flow_id(lchip, pKey->data, p_data);
                }
                qid = SYS_ACL_FIELD_XD_IM_FLOW_ID + pKey->type - CTC_FIELD_KEY_NPM_IM_FLOW_ID;
                if ((CTC_FIELD_KEY_NPM_IM_D_FLAG == pKey->type) && (0 == pe->im_d_flag))
                {
                    qid = SYS_ACL_FIELD_XD_IM_SRC_D_FLAG;
                }
            }
            else if (is_ipv6 && flow_type != 0) /* transmit and sink node, ipv6 use parser data hit flowid */
            {
                qid = SYS_ACL_FIELD_IPV6_IM_FLOW_ID + pKey->type - CTC_FIELD_KEY_NPM_IM_FLOW_ID;
            }
        }
        else if(pKey->type == CTC_FIELD_KEY_NPM_IM_FLOW_ID)
        {
            value = ((1<<28) | (pKey->data<<8) | npm_im_flow.fih_type);
            mask = 0x1FFFFFFF ;
        }
        if(pKey->type == CTC_FIELD_KEY_NPM_IM_L_FLAG)
        {
            pe->im_l_flag = pKey->data&pKey->mask;
        }
        else if(pKey->type == CTC_FIELD_KEY_NPM_IM_D_FLAG)
        {
            pe->im_d_flag = pKey->data&pKey->mask;
        }
        break;
    case CTC_FIELD_KEY_SRV6_SRH_FLAG:
        if ((DRV_IS_TMM(lchip) || DRV_IS_TMG(lchip)))
        {
            pKey->data |= (1<<26)|(p_usw_acl_master[lchip]->srv6_rsv_udf_idx<<16);
            pKey->mask |= 0x7FF<<16;
        }
        break;
    case CTC_FIELD_KEY_L4_DST_PORT:
        qid = (CTC_ACL_KSET_ISSET(fk->kset, qid) ? qid : SYS_ACL_FIELD_L4_DP_SHARE);
        break;
    case CTC_FIELD_KEY_L4_SRC_PORT:
        qid = (CTC_ACL_KSET_ISSET(fk->kset, qid) ? qid : SYS_ACL_FIELD_L4_SP_SHARE);
        break;
    case CTC_FIELD_KEY_VN_ID:
        qid = (CTC_ACL_KSET_ISSET(fk->kset, qid) ? qid : SYS_ACL_FIELD_VN_ID_SHARE);
        break;
    case CTC_FIELD_KEY_VXLAN_FLAGS:
        qid = (CTC_ACL_KSET_ISSET(fk->kset, qid) ? qid : SYS_ACL_FIELD_VXLAN_FLAGS_SHARE);
        break;
    case CTC_FIELD_KEY_LOGIC_SRC_PORT:
        if(pe->group->group_info.dir == CTC_EGRESS)
        {
            qid = CTC_FIELD_KEY_FID_CID;
        }
        else if(CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_LOGIC_PORT_SHARE) )
        {
            qid = SYS_ACL_FIELD_LOGIC_PORT_SHARE;
            value = ((DRV_ENUM(DRV_FLOWPORTTYPE_LPORT) <<16) | pKey->data) ;
            mask =  ((3<<16) | pKey->mask) ;
        }
        break;
    case CTC_FIELD_KEY_LOGIC_DST_PORT:
        if(CTC_EGRESS == pe->group->group_info.dir)
        {
            if(CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_LOGIC_PORT_SHARE))
            {
                qid = SYS_ACL_FIELD_LOGIC_PORT_SHARE;
                value = ((DRV_ENUM(DRV_FLOWPORTTYPE_LPORT) <<16) | pKey->data) ;
                mask =  ((3<<16) | pKey->mask) ;
            }
            else
            {
                qid = CTC_FIELD_KEY_LOGIC_SRC_PORT;
            }
        }
        break;
    case CTC_FIELD_KEY_DST_CID:
    case CTC_FIELD_KEY_SRC_CID:
        value = is_add? ((1<<16) | pKey->data) : 0;
        mask = is_add? ((1<<16) | pKey->mask) : 0;
        break;
    case CTC_FIELD_KEY_STK_SRC_GPORT:
    case CTC_FIELD_KEY_SRC_GPORT:
        value = SYS_MAP_CTC_GPORT_TO_DRV_GPORT(pKey->data);
        break;
    case CTC_FIELD_KEY_STK_DST_GPORT:
    {
        uint32 dst_port = 0;
        uint32 dest_map = 0;
        uint32 dest_chipid = 0;
        uint32 dest_id = 0;

        if (is_add)
        {
            dst_port = pKey->data;
            dest_chipid = SYS_MAP_CTC_GPORT_TO_GCHIP(dst_port);
            dest_id  = SYS_MAP_CTC_GPORT_TO_DRV_LPORT(dst_port);
            dest_map = SYS_ENCODE_DESTMAP(dest_chipid, dest_id);
        }
        value = dest_map ;
        mask = pKey->mask?pKey->mask:0xFFFFFFFF;
    }
        break;
    case CTC_FIELD_KEY_SNIFFING_PKT:
        value = is_add? (pKey->data ? 1 : 0) : 0;
        mask = is_add? (2 | (pKey->mask & 1)) : 0;
        break;
    case CTC_FIELD_KEY_NAT_IFTYPE:
        value = is_add? (pKey->data ? 1 : 0) : 0;
        mask = is_add? (pKey->mask & 1) : 0;
        break;
    case CTC_FIELD_KEY_LEN_ERROR:
        value = (pKey->data) | (1 << 6);
        mask = (pKey->mask) | (1 << 6);
        break;
    default:
        break;
    }

    if (qid == CTC_FIELD_KEY_PORT || qid == CTC_FIELD_KEY_PORT_BITMAP)
    {
        ctc_field_port_t* p_field_port = (ctc_field_port_t*)pKey->ext_data;
        uint32* port_bitmap_data = NULL;
        drv_acl_port_info_t  port_drv;
        uint32 mask_arr[CTC_PORT_BITMAP_IN_WORD+1] = {0};
        uint32 data_arr[CTC_PORT_BITMAP_IN_WORD+1] = {0};
        uint8  is_port_bitmap = 0;

        if(is_add || p_field_port)
        {
            if (qid == CTC_FIELD_KEY_PORT)
            {
                is_port_bitmap = (p_field_port->type == CTC_FIELD_PORT_TYPE_PORT_BITMAP && !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_PBM));
                port_bitmap_data = (uint32*)p_field_port->port_bitmap;
            }
            else
            {
                is_port_bitmap = TRUE;
                port_bitmap_data = pKey->ext_data;
            }
          if (is_port_bitmap)
          {
            uint8 port_bitmap_base = 0;
            uint8 valid = 0;
            uint8 width = 0;
            uint8 loop2 = 0;

            sal_memcpy(data_arr, port_bitmap_data, sizeof(ctc_port_bitmap_t));
            for(loop=0; loop<CTC_PORT_BITMAP_IN_WORD; loop++)
            {
                mask_arr[loop] = ~port_bitmap_data[loop];
            }

            if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_PORT_BITMAP_MODE0))
            {
                width = 1;
                qid = SYS_ACL_FIELD_PORT_BITMAP_MODE0;
            }
            else if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_PORT_BITMAP_MODE1))
            {
                width = 2;
                qid = SYS_ACL_FIELD_PORT_BITMAP_MODE1;
            }
            else if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_PORT_BITMAP_MODE2))
            {
                width = 4;
                qid = SYS_ACL_FIELD_PORT_BITMAP_MODE2;
            }
            else if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_PORT_BITMAP_MODE3))
            {
                width = 8;
                qid = SYS_ACL_FIELD_PORT_BITMAP_MODE3;
            }
            else if(is_add)
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
                return CTC_E_NOT_SUPPORT;
            }

            if(is_add && width<8)
            {
                for(loop=0; loop<CTC_PORT_BITMAP_IN_WORD; loop+= width)
                {
                    loop2 = 0;
                    while(loop2 < width && !port_bitmap_data[loop + loop2])loop2++;
                    if(loop2 < width)
                    {
                        if(valid)
                        {
                            return CTC_E_INVALID_PARAM;
                        }
                        port_bitmap_base = loop;
                        valid = 1;
                    }
                }
                data_arr[port_bitmap_base+width] = (port_bitmap_base/width) & 0xf;
                mask_arr[port_bitmap_base+width] = 0xf;
            }
            p_data = &data_arr[port_bitmap_base];
            p_mask = &mask_arr[port_bitmap_base];
        }
        else
        {
            sal_memset(&port_drv, 0, sizeof(port_drv));
            if (is_add)
            {
                port_drv.dir = CTC_INGRESS;
                CTC_ERROR_RETURN(_sys_usw_acl_build_port_info(lchip, pe->key_type, pKey, &port_drv));
            }

            switch(p_field_port->type)
            {
            case CTC_FIELD_PORT_TYPE_PORT_CLASS:
            case CTC_FIELD_PORT_TYPE_VLAN_CLASS:
            case CTC_FIELD_PORT_TYPE_L3IF_CLASS:
            case CTC_FIELD_PORT_TYPE_PBR_CLASS:
                qid = CTC_FIELD_KEY_CLASS_ID;
                value = port_drv.class_id_data;
                mask  = port_drv.class_id_mask;
                break;
            case CTC_FIELD_PORT_TYPE_LOGIC_PORT:
                qid = CTC_FIELD_KEY_LOGIC_SRC_PORT;
                if(CTC_ACL_KSET_ISSET(fk->kset, qid))
                {
                    if(CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_GPORT))
                    {
                        return CTC_E_PARAM_CONFLICT;
                    }
                }
                else
                {
                    SYS_ACL_CHECK_UNION_FLAG_CONFLICT(pe->key_port_mode, SYS_ACL_KEY_SHARE_MODE_LOGIC_PORT);
                    qid = SYS_ACL_FIELD_LOGIC_PORT_SHARE;
                }
                value = port_drv.gport;
                mask  = port_drv.gport_mask;
                break;
            case CTC_FIELD_PORT_TYPE_GPORT:
                qid = CTC_FIELD_KEY_PORT;
                if(CTC_ACL_KSET_ISSET(fk->kset, qid))
                {
                    if(CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_LOGIC_PORT))
                    {
                        return CTC_E_PARAM_CONFLICT;
                    }
                }
                else
                {
                    SYS_ACL_CHECK_UNION_FLAG_CONFLICT(pe->key_port_mode, SYS_ACL_KEY_SHARE_MODE_PORT);
                    qid = SYS_ACL_FIELD_GPORT_SHARE;
                }
                value = port_drv.gport;
                mask  = port_drv.gport_mask;
                break;
            case CTC_FIELD_PORT_TYPE_PORT_BITMAP:
                SYS_ACL_CHECK_UNION_FLAG_CONFLICT(pe->key_port_mode, SYS_ACL_KEY_SHARE_MODE_PBM);    /*0:none,1:port,2:metadata,3:port_bmp*/
                qid = SYS_ACL_FIELD_PBM;
                value = (port_drv.gport_type << 20) | (port_drv.bitmap_base<< 16) | (port_drv.bitmap_64[0]&0xFFFF);
                mask  = (port_drv.gport_type_mask << 20) | (port_drv.bitmap_base_mask<< 16) | (port_drv.bitmap_64_mask[0]&0xFFFF);
                break;
            default:
                return 0;
            }
            
            if(qid == SYS_ACL_FIELD_LOGIC_PORT_SHARE || qid == SYS_ACL_FIELD_GPORT_SHARE)
            {
                value |= (port_drv.gport_type << 16) ;
                mask  |= (port_drv.gport_type_mask << 16) ;
            }
          }
        }
        else
        {
            qid = CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_SYS_BMP_MODEX) ?
                  (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_PORT_BITMAP_MODE0) ? SYS_ACL_FIELD_PORT_BITMAP_MODE0 :
                  CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_PORT_BITMAP_MODE1) ? SYS_ACL_FIELD_PORT_BITMAP_MODE1 :
                  CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_PORT_BITMAP_MODE2) ? SYS_ACL_FIELD_PORT_BITMAP_MODE2 : SYS_ACL_FIELD_PORT_BITMAP_MODE3 ) :
                  (SYS_ACL_KEY_SHARE_MODE_PBM == pe->key_port_mode) ? SYS_ACL_FIELD_PBM :
                  (SYS_ACL_KEY_SHARE_MODE_PORT == pe->key_port_mode) ? SYS_ACL_FIELD_GPORT_SHARE :
                  (SYS_ACL_KEY_SHARE_MODE_LOGIC_PORT == pe->key_port_mode) ? SYS_ACL_FIELD_LOGIC_PORT_SHARE :
                  CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_CLASS_ID) ? CTC_FIELD_KEY_CLASS_ID :
                  CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_LOGIC_PORT) ? CTC_FIELD_KEY_LOGIC_SRC_PORT :
                  CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_GPORT) ? CTC_FIELD_KEY_PORT : SYS_ACL_FIELD_COUNT;
            if(qid == SYS_ACL_FIELD_COUNT)
            {
                return 0;
            }
        }
      CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));
      port_qid = qid;
    }
    else if (qid == CTC_FIELD_KEY_SUDF || qid == CTC_FIELD_KEY_SUDF_AD_ID)
    {
        ctc_acl_udf_t* p_udf_info_data = (ctc_acl_udf_t*)pKey->ext_data;
        ctc_acl_udf_t* p_udf_info_mask = (ctc_acl_udf_t*)pKey->ext_mask;
        sys_acl_udf_entry_t* p_udf_entry;
        uint16*    p_udf = NULL;
        uint32     udf_loop = 0;
        uint8      udf1_valid = 0;
        uint8      offset_valid = 0;
        uint16    udf1_hit_idx = 0;        
        uint32     hw_udf[4] = {0};
        uint32     hw_udf_mask[4] = {0};
        uint8      granularity = 1;
        uint8      hw_shift = 0;

        if(is_add)
        {
            if (qid == CTC_FIELD_KEY_SUDF_AD_ID)
            {
                udf1_hit_idx = p_udf_info_data->udf_ad_id;/*AT mult pp*/
                udf1_hit_idx += (p_udf_info_data->udf_ad_id ? (MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MIN) - 1): 0);/*tmm udf-ad-id can assign 0 for srv6*/
                CTC_MAX_VALUE_CHECK(udf1_hit_idx, MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MAX));
                if(p_udf_info_data->udf_ad_id)
                {
                    _sys_usw_acl_get_udf_entry_by_ad_id(lchip, udf1_hit_idx, &p_udf_entry);
                    if(p_udf_entry == NULL)
                    {
                        return CTC_E_NOT_EXIST;
                    }
                    granularity = p_udf_entry->granularity;
                }
                p_udf = (uint16*)p_udf_info_mask->udf;
                for(udf_loop = 0; udf_loop < CTC_ACL_UDF_FIELD_NUM; udf_loop++)
                {
                    if(p_udf[udf_loop] != 0)
                    {
                        CTC_BIT_SET(offset_valid, udf_loop);
                    }
                }
            }
            else
            {
                _sys_usw_acl_get_udf_info(lchip, p_udf_info_data->udf_id, &p_udf_entry);
                if (!p_udf_entry || !p_udf_entry->key_index_used)
                {
                    return CTC_E_NOT_EXIST;
                }
                if (!p_udf_entry->is_sudf)
                {
                    return CTC_E_INVALID_PARAM;
                }
                offset_valid = (1<<p_udf_entry->udf_offset_num)-1;
                udf1_hit_idx = p_udf_entry->udf_hit_index;
                granularity = p_udf_entry->granularity;
            }

            for (udf_loop=SYS_ACL_FIELD_UDF1_CHUNK0; udf_loop<=SYS_ACL_FIELD_UDF1_CHUNK7; udf_loop++)
            {
                CTC_ACL_KSET_ISSET(fk->kset, udf_loop) ?
                CTC_BIT_SET(udf1_valid, udf_loop-SYS_ACL_FIELD_UDF1_CHUNK0) :
                CTC_BIT_UNSET(udf1_valid, udf_loop-SYS_ACL_FIELD_UDF1_CHUNK0);
            }
            if (!udf1_valid)
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
                return CTC_E_NOT_SUPPORT;
            }

            udf1_valid &= (offset_valid&0xFF);

            p_data = &value;
            p_mask = &mask;
            if (udf1_valid != 0)
            {
                value = udf1_hit_idx;
                mask = 0x3FF;
                CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF1_PROF_ID,  p_data, p_mask, is_add));

                SYS_USW_SET_HW_UDF(granularity, hw_udf, p_udf_info_data->udf);
                SYS_USW_SET_HW_UDF(granularity, hw_udf_mask, p_udf_info_mask->udf);
                for (loop=0; loop<CTC_ACL_UDF_FIELD_NUM; loop++)
                {
                    hw_shift = ((loop&0x01)* 16);
                    if (!CTC_IS_BIT_SET(udf1_valid, loop))
                    {
                        continue;
                    }
                    value = (hw_udf[loop/2] >> hw_shift) & 0XFFFF; /*((uint16*)hw_udf + loop);*/
                    mask = (hw_udf_mask[loop/2] >> hw_shift) & 0XFFFF; /*((uint16*)hw_udf_mask + loop);*/
                    p_data = &value;
                    p_mask = &mask;
                    CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF1_CHUNK0+loop,  p_data, p_mask, is_add));
                    if(mask == 0)
                    {
                        CTC_BIT_UNSET(udf1_valid, loop);
                    }
                }
                value = udf1_valid;
                mask  = udf1_valid;
                CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF1_VALID_BITMAP,  p_data, p_mask, is_add));

            }
            udf1_valid &= (offset_valid&0xFF);
            value = udf1_valid;
            mask  = udf1_valid;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF1_VALID_BITMAP,  p_data, p_mask, is_add));

        }
        else
        {
            if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_UDF1_PROF_ID))
            {
                CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF1_PROF_ID,  p_data, p_mask, is_add));
            }

            if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_UDF1_VALID_BITMAP))
            {
                CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF1_VALID_BITMAP,  p_data, p_mask, is_add));
            }

            for (loop=0; loop<CTC_ACL_UDF_FIELD_NUM; loop++)
            {
                if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_UDF1_CHUNK0+loop))
                {
                    CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF1_CHUNK0+loop,  p_data, p_mask, is_add));
                }
            }
        }
    }
    else if (qid == CTC_FIELD_KEY_UDF || qid == CTC_FIELD_KEY_UDF_AD_ID)
    {
        ctc_acl_udf_t* p_udf_info_data = (ctc_acl_udf_t*)pKey->ext_data;
        ctc_acl_udf_t* p_udf_info_mask = (ctc_acl_udf_t*)pKey->ext_mask;
        sys_acl_udf_entry_t* p_udf_entry;
        uint16*    p_udf = NULL;
        uint32     udf_loop = 0;
        uint8      udf2_valid = 0;
        uint8   offset_valid = 0;
        uint16    udf2_hit_idx = 0;
        uint32     hw_udf[4] = {0};
        uint32     hw_udf_mask[4] = {0};
        uint8      granularity = 1;
        uint8      hw_shift = 0;

        if (is_add)
        {
            if (qid == CTC_FIELD_KEY_UDF_AD_ID)
            {
                CTC_MAX_VALUE_CHECK(p_udf_info_data->udf_ad_id, MCHIP_CAP(SYS_CAP_ACL_UDF_AD_ID_MAX));
                /* tmm udf-ad-id can assign 0 for srv6, 0 means invalid udf-ad-id */
                if(p_udf_info_data->udf_ad_id)
                {
                    _sys_usw_acl_get_udf_entry_by_ad_id(lchip, p_udf_info_data->udf_ad_id, &p_udf_entry);
                    if(p_udf_entry == NULL)
                    {
                        return CTC_E_NOT_EXIST;
                    }
                    granularity = p_udf_entry->granularity;
                }
                p_udf = (uint16*)p_udf_info_mask->udf;
                for(udf_loop = 0; udf_loop < CTC_ACL_UDF_FIELD_NUM; udf_loop++)
                {
                    if(p_udf[udf_loop] != 0)
                    {
                        CTC_BIT_SET(offset_valid, udf_loop);
                    }
                }
                udf2_hit_idx = p_udf_info_data->udf_ad_id;
            }
            else
            {
                _sys_usw_acl_get_udf_info(lchip, p_udf_info_data->udf_id, &p_udf_entry);
                if (!p_udf_entry || !p_udf_entry->key_index_used)
                {
                    return CTC_E_NOT_EXIST;
                }
                if (p_udf_entry->is_sudf)
                {
                    return CTC_E_INVALID_PARAM;
                }
                offset_valid = (1<<p_udf_entry->udf_offset_num)-1;
                udf2_hit_idx = p_udf_entry->udf_hit_index;
                granularity = p_udf_entry->granularity;
            }

            for (udf_loop=SYS_ACL_FIELD_UDF2_CHUNK0; udf_loop<=SYS_ACL_FIELD_UDF2_CHUNK7; udf_loop++)
            {
                CTC_ACL_KSET_ISSET(fk->kset, udf_loop) ?
                CTC_BIT_SET(udf2_valid, udf_loop-SYS_ACL_FIELD_UDF2_CHUNK0) :
                CTC_BIT_UNSET(udf2_valid, udf_loop-SYS_ACL_FIELD_UDF2_CHUNK0);
            }
            if (!udf2_valid)
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
                return CTC_E_NOT_SUPPORT;
            }

            p_data = &value;
            p_mask = &mask;
            value = udf2_hit_idx;
            mask = 0x3FF;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF2_PROF_ID,  p_data, p_mask, is_add));

            SYS_USW_SET_HW_UDF(granularity, hw_udf, p_udf_info_data->udf);
            SYS_USW_SET_HW_UDF(granularity, hw_udf_mask, p_udf_info_mask->udf);
            for (loop = 0; loop < CTC_ACL_UDF_FIELD_NUM; loop++)
            {
                hw_shift = ((loop&0x01)* 16);
                if (!CTC_IS_BIT_SET(udf2_valid, loop))
                {
                    continue;
                }
                value = (hw_udf[loop/2] >> hw_shift) & 0XFFFF;     /*((uint16*)hw_udf + loop);*/
                mask = (hw_udf_mask[loop/2] >> hw_shift) & 0XFFFF; /*((uint16*)hw_udf_mask + loop);*/
                p_data = &value;
                p_mask = &mask;
                CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF2_CHUNK0 + loop,  p_data, p_mask, is_add));
                if (mask == 0)
                {
                    CTC_BIT_UNSET(offset_valid, loop);
                }
                value = udf2_valid;
                mask  = udf2_valid;
                CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF2_VALID_BITMAP,  p_data, p_mask, is_add));
            }
            udf2_valid &= (offset_valid&0xFF);
            value = udf2_valid;
            mask  = udf2_valid;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF2_VALID_BITMAP,  p_data, p_mask, is_add));
        }
        else
        {
            if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_UDF2_PROF_ID))
            {
                CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF2_PROF_ID,  p_data, p_mask, is_add));
            }

            if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_UDF2_VALID_BITMAP))
            {
                CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF2_VALID_BITMAP,  p_data, p_mask, is_add));
            }

            for (loop=0; loop<CTC_ACL_UDF_FIELD_NUM; loop++)
            {
                if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_UDF2_CHUNK0+loop))
                {
                    CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_UDF2_CHUNK0+loop,  p_data, p_mask, is_add));
                }
            }
        }
    }
    else if (qid == CTC_FIELD_KEY_IS_Y1731_OAM)
    {
        p_data = &value;
        p_mask = &mask;
        mask = is_add? 0xF: 0;

        qid = CTC_FIELD_KEY_L4_TYPE;
        value = is_add? CTC_PARSER_L4_TYPE_ACH_OAM: 0;
        CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));
        qid = CTC_FIELD_KEY_L4_USER_TYPE;
        value = is_add? CTC_PARSER_L4_USER_TYPE_ACHOAM_ACHY1731: 0;
        CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));
    }
    else if (qid == CTC_FIELD_KEY_DST_NHID)
    {
        uint32 nhid = 0;
        uint32 dest_map = 0;
        uint32 nh_offset = 0;
        uint8 dest_is_aps = 0;
        uint8 dest_is_ecmp = 0;
        uint8 match_nh_offset = 0;
        sys_nh_info_dsnh_t  nhinfo = {0};

        if (pKey->ext_data && (0 == pKey->data) && (0 == pKey->mask))
        {
            nh_offset = is_add?*(uint32*)pKey->ext_data:0;
            match_nh_offset = 1;
        }
        else if (is_add)
        {
            nhid = pKey->data;
            sal_memset(&nhinfo, 0, sizeof(sys_nh_info_dsnh_t));
            CTC_ERROR_RETURN(sys_usw_nh_get_nhinfo(lchip, nhid, &nhinfo, 0));
            nh_offset = nhinfo.dsnh_offset;

            if(SYS_NH_TYPE_OVERLAY == nhinfo.nh_entry_type && DRV_IS_AT(lchip) && dir == CTC_INGRESS)
            {
                return CTC_E_NOT_SUPPORT;
            }

            if (nhinfo.ecmp_valid || nhinfo.is_ecmp_intf)
            {
                dest_map = nhinfo.ecmp_group_id;
                dest_is_ecmp = 1;
            }
            else
            {
                dest_map = nhinfo.dest_map;
                if (nhinfo.aps_en)
                {
                    dest_is_aps = 1;
                }
                else if(nhinfo.arp_id && nhinfo.use_destmap_profile)
                {
                    uint32 cmd = DRV_IOR(DsDestMapProfileUc_t, DsDestMapProfileUc_destMap_f);
                    CTC_ERROR_RETURN(DRV_FIELD_IOCTL(lchip, (dest_map&0xffff), DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &dest_map));
                }
            }
        }

        if (0 == match_nh_offset)
        {
            qid = SYS_ACL_FIELD_DESTMAP;
            value = dest_map ;
            mask = is_add? ((dest_is_aps||dest_is_ecmp)?0xFFF:0xFFFFFFFF): 0;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

            qid = SYS_ACL_FIELD_NH_EXT;
            value = nhinfo.nexthop_ext;
            mask = is_add? 1: 0;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

            qid = SYS_ACL_FIELD_DEST_APS;
            value = dest_is_aps;
            mask = is_add? 1: 0;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

            qid = SYS_ACL_FIELD_DEST_ECMP;
            value = dest_is_ecmp;
            mask = is_add? 1: 0;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));
        }

        qid = SYS_ACL_FIELD_NH_OFFSET;
        value = nh_offset;
        mask = (match_nh_offset)?*(uint32*)pKey->ext_mask:(is_add && !nhinfo.aps_en? 0xFFFFFFFF: 0);
        CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

    }
    else if (qid == CTC_FIELD_KEY_DST_GPORT)
    {
        uint32 dst_port = 0;
        uint32 dest_map = 0;
        uint32 dest_chipid = 0;
        uint32 dest_id = 0;

        if (is_add)
        {
            dst_port = pKey->data;
            dest_chipid = SYS_MAP_CTC_GPORT_TO_GCHIP(dst_port);
            dest_id  = SYS_MAP_CTC_GPORT_TO_DRV_LPORT(dst_port);
            dest_map = SYS_ENCODE_DESTMAP(dest_chipid, dest_id);
        }

        qid = SYS_ACL_FIELD_DESTMAP;
        value = dest_map ;
        mask = is_add? 0xFFFFFFFF: 0;
        CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

        qid = SYS_ACL_FIELD_DEST_APS;
        value = 0;
        mask = is_add? 1: 0;
        CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

        qid = SYS_ACL_FIELD_DEST_ECMP;
        value = 0;
        mask = is_add? 1: 0;
        CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

        // SYS_USW_ACL_SET_BMP(pe->key_bmp, CTC_FIELD_KEY_DST_GPORT, is_add);

    }
    else if (qid == CTC_FIELD_KEY_CPU_REASON_ID)
    {
        uint8 excp_gid = 0;
        uint8 excp_en = 0;
        uint8 fatal_en = 0;
        uint8 fatal_excp_idx = 0;
        uint32 igmp_mode = 0;

        sys_usw_global_ctl_get(lchip, CTC_GLOBAL_IGMP_SNOOPING_MODE, &igmp_mode);
        if (is_add)
        {
            sys_cpu_reason_info_t  reason_info;
            sal_memset(&reason_info, 0, sizeof(sys_cpu_reason_info_t));
            if (!pKey->ext_data)
            {
                reason_info.reason_id = pKey->data;
                reason_info.acl_key_valid = 1;
                CTC_ERROR_RETURN(sys_usw_cpu_reason_get_reason_info(lchip, pe->group->group_info.dir, &reason_info));
                /*
                if (!(reason_info.fatal_excp_valid || reason_info.gid_valid))//????
                {
                    CTC_ERROR_RETURN(CTC_E_BADID);
                }
                */
                pe->igmp_snooping = CTC_PKT_CPU_REASON_IGMP_SNOOPING == pKey->data;
            }
            else
            {
                reason_info.gid_for_acl_key = pKey->data;
                reason_info.gid_valid = 1;
            }
            if((!pe->igmp_snooping || igmp_mode != CTC_GLOBAL_IGMP_SNOOPING_MODE_2)
                && reason_info.fatal_excp_valid )
            {
                fatal_en = reason_info.fatal_excp_valid;
                fatal_excp_idx = reason_info.fatal_excp_index;
            }
            else if (!reason_info.gid_valid)
            {
                return CTC_E_BADID;
            }
            else
            {
                excp_en = !pKey->ext_data? reason_info.gid_valid: 1;
                excp_gid = reason_info.gid_for_acl_key;
            }
        }

        qid = SYS_ACL_FIELD_EXCEPTION_EXCP_EN;
        value = excp_en ;
        mask = is_add && excp_en ? 1: 0;
        CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

        qid = SYS_ACL_FIELD_EXCEPTION_GID;
        value = excp_gid;
        mask = is_add && excp_en? pKey->mask & 0xFF: 0;
        CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

        if(pe->group->group_info.dir == CTC_INGRESS)
        {
            qid = SYS_ACL_FIELD_EXCEPTION_FATAL_EN;
            value = fatal_en;
            mask = is_add && fatal_en ? 1: 0;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));

            qid = SYS_ACL_FIELD_EXCEPTION_FATAL_EXCP_ID;
            value = fatal_excp_idx;
            mask = is_add && fatal_en ? pKey->mask & 0xF: 0;
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));
        }

    }
    else if (qid == CTC_FIELD_KEY_IPV6_DA)
    {
        if (is_add &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK0) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK1) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK2) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK3))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
            return CTC_E_NOT_SUPPORT;
        }
        else if (is_add)
        {
            SYS_SET_HW_IP6(value_arr, (uint32*)(pKey->ext_data));
            SYS_SET_HW_IP6(mask_arr, (uint32*)(pKey->ext_mask));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK0))
        {
            p_data = &value_arr[0];
            p_mask = &mask_arr[0];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_IPV6_DA_CHUNK0, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK1))
        {
            p_data = &value_arr[1];
            p_mask = &mask_arr[1];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_IPV6_DA_CHUNK1, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK2))
        {
            p_data = &value_arr[2];
            p_mask = &mask_arr[2];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_IPV6_DA_CHUNK2, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK3))
        {
            p_data = &value_arr[3];
            p_mask = &mask_arr[3];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_IPV6_DA_CHUNK3, p_data, p_mask, is_add));
        }
    }
    else if (qid == CTC_FIELD_KEY_IPV6_SA)
    {
        if (is_add &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK0) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK1) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK2) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK3))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
            return CTC_E_NOT_SUPPORT;
        }
        else if (is_add)
        {
            SYS_SET_HW_IP6(value_arr, (uint32*)(pKey->ext_data));
            SYS_SET_HW_IP6(mask_arr, (uint32*)(pKey->ext_mask));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK0))
        {
            p_data = &value_arr[0];
            p_mask = &mask_arr[0];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_IPV6_SA_CHUNK0, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK1))
        {
            p_data = &value_arr[1];
            p_mask = &mask_arr[1];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_IPV6_SA_CHUNK1, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK2))
        {
            p_data = &value_arr[2];
            p_mask = &mask_arr[2];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_IPV6_SA_CHUNK2, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK3))
        {
            p_data = &value_arr[3];
            p_mask = &mask_arr[3];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_IPV6_SA_CHUNK3, p_data, p_mask, is_add));
        }
    }
#ifdef nsh
    else if (qid == CTC_FIELD_KEY_NSH_CONTEXT_HEADER)
    {
        if (is_add &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_NSH_CH_CHUNK0) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_NSH_CH_CHUNK1) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_NSH_CH_CHUNK2) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_NSH_CH_CHUNK3))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "qid:%d not support, qstr:%s\n", qid, ifp_field_str[qid]);
            return CTC_E_NOT_SUPPORT;
        }
        SYS_ACL_WORD_REVERSE(value_arr, pKey->ext_data, 4*sizeof(uint32));
        SYS_ACL_WORD_REVERSE(mask_arr,  pKey->ext_mask, 4*sizeof(uint32));
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_NSH_CH_CHUNK0))
        {
            p_data = &value_arr[0];
            p_mask = &mask_arr[0];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_NSH_CH_CHUNK0, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_NSH_CH_CHUNK1))
        {
            p_data = &value_arr[1];
            p_mask = &mask_arr[1];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_NSH_CH_CHUNK1, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_NSH_CH_CHUNK2))
        {
            p_data = &value_arr[2];
            p_mask = &mask_arr[2];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_NSH_CH_CHUNK2, p_data, p_mask, is_add));
        }
        if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_NSH_CH_CHUNK3))
        {
            p_data = &value_arr[3];
            p_mask = &mask_arr[3];
            CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, SYS_ACL_FIELD_NSH_CH_CHUNK3, p_data, p_mask, is_add));
        }
        // SYS_USW_ACL_SET_BMP(pe->key_bmp, CTC_FIELD_KEY_NSH_CONTEXT_HEADER, is_add);
    }
#endif
    else if ( qid == CTC_FIELD_KEY_VLAN_XLATE_HIT )
    {
        uint32 scl_id = 0;
        CTC_PTR_VALID_CHECK(pKey->ext_data);
        scl_id = *(uint32*)(pKey->ext_data);
        CTC_MAX_VALUE_CHECK(scl_id, 1);
        qid = SYS_ACL_FIELD_VLAN_XLATE0_HIT+scl_id;
        value = (is_add && pKey->data == 0)? 1: 0;
        mask = is_add? (pKey->mask & 0x1): 0;
        p_data = &value;
        p_mask = &mask;
        CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add), ret, error_proc);
    }
    else if ( qid == CTC_FIELD_KEY_XDATA )
    {
        int8*   f_sel = NULL;
        uint8   is_stk = pe->key_type == CTC_ACL_KEY_STK_HDR;
        uint32 qid_start = SYS_ACL_FIELD_XDATA_DW0;
        uint32 qid_end   = SYS_ACL_FIELD_XDATA_B3;
        ctc_xdata_t* xdata = (ctc_xdata_t*)pKey->ext_data;
        uint8 width[10] = {4, 4, 2, 2, 2, 2, 1, 1, 1, 1};   /* unit is btye */
        f_sel = fk->xdata_sel;
        CTC_PTR_VALID_CHECK(xdata);

        if(is_stk)
        {
            qid_start = SYS_ACL_FIELD_STK_XDATA_DW0;
            qid_end   = SYS_ACL_FIELD_STK_XDATA_B3;
            width[4] = 1;
            width[5] = 1;
        }
        
        if(is_add)
        {
            uint8 stk_offset[8] = {0, 1, 0, 1, 4, 5, 6, 7}; /* unit is def by width */
            uint8 bit_offset = 0;
            int8 offset = 0;
            uint32 xd[4] = {0};
            uint32 xm[4] = {0};
            uint16 data_valid_bmp  = 0;
            uint16 field_valid_bmp = 0;
            for(loop = 0; loop < xdata->num; loop++)
            {
                ctc_xdata_field_t* x_field = &xdata->fields[loop];
                CTC_BMP_COPY_RANGE(xd, x_field->offset, &x_field->data,  0,  x_field->width);
                CTC_BMP_COPY_RANGE(xm, x_field->offset, &x_field->mask,  0,  x_field->width);
                data_valid_bmp |= ((1 << (x_field->width/8))-1)<<(x_field->offset/8);
            }
            /* map xdata field bmp in flex key */
            for(qid = qid_start; qid <= qid_end; qid++)
            {
                if(!CTC_ACL_KSET_ISSET(fk->kset, qid))
                {
                    continue;
                }
                loop = qid-qid_start;
                offset = is_stk ? stk_offset[loop] : f_sel[loop];
                {
                    field_valid_bmp |= (((1 << width[loop]) - 1) << (offset*width[loop]));
                }
            }
            /* config data is out of the flex key range */
            if(data_valid_bmp & (~field_valid_bmp))
            {
                return CTC_E_INVALID_CONFIG;
            }

            for(qid = qid_start; qid <= qid_end; qid++)
            {
                if(!CTC_ACL_KSET_ISSET(fk->kset, qid))
                {
                    continue;
                }
                loop = qid-qid_start;
                offset = is_stk ? stk_offset[loop] : f_sel[loop];
                {
                    if(width[loop] == 4)
                    {
                        value = xd[offset];
                        mask  = xm[offset];
                    }
                    else if(width[loop] == 2)
                    {
                        bit_offset = offset % 2;
                        bit_offset *= CTC_XDATA_WIDTH_16;
                        value = (xd[offset >> 1] >> bit_offset)&0xffff;
                        mask  = (xm[offset >> 1] >> bit_offset)&0xffff;
                    }
                    else
                    {
                        bit_offset = offset % 4;
                        bit_offset *= CTC_XDATA_WIDTH_8;
                        value = (xd[offset>>2] >> bit_offset)&0xff;
                        mask  = (xm[offset>>2] >> bit_offset)&0xff;
                    }
                    CTC_ERROR_RETURN(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add));
                }
            }
        }
        else
        {
            for(qid = qid_start; qid <= qid_end; qid++)
            {
                if(CTC_ACL_KSET_ISSET(fk->kset, qid))
                {
                    CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, 0), ret, error_proc);
                }
            }
        }
    }
    else if(qid == CTC_FIELD_KEY_STK_HDR_EXIST)
    {
        CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add), ret, error_proc);

        qid = SYS_ACL_FIELD_STK_HDR_BMP;
        if(CTC_ACL_KSET_ISSET(fk->kset, qid) && ((is_add && pKey->ext_data) || !is_add))
        {
            value = pKey->ext_data ? (*(uint32*)pKey->ext_data)& 0x1F : 0;
            mask = pKey->ext_mask ? (*(uint32*)pKey->ext_mask)& 0x1F: (pKey->ext_data ? 0x1F: 0);
            CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add), ret, error_proc);
        }
    }
    else if(1 == p_usw_acl_master[SYS_PP_BASE(lchip)]->ifa_ver && (qid == CTC_FIELD_KEY_INT_TYPE || qid == CTC_FIELD_KEY_INT_VER || qid == CTC_FIELD_KEY_INT_HOP_ML ||\
            qid == CTC_FIELD_KEY_INT_REMAIN_HOP_CNT || qid == CTC_FIELD_KEY_INT_INSTRUCTION))
    {
        qid = (qid == CTC_FIELD_KEY_INT_TYPE) ? SYS_ACL_FIELD_INT_TYPE : ((qid == CTC_FIELD_KEY_INT_VER) ? SYS_ACL_FIELD_INT_VER :
            (((qid == CTC_FIELD_KEY_INT_HOP_ML) ? SYS_ACL_FIELD_INT_HOP_ML :((qid == CTC_FIELD_KEY_INT_REMAIN_HOP_CNT) ? SYS_ACL_FIELD_INT_REMAIN_HOP_CNT : SYS_ACL_FIELD_INT_INSTRUCTION))));
        CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add), ret, error_proc);
    }
    else if(qid == CTC_FIELD_KEY_IP_FRAG)
    {
        if (is_add)
        {
            CTC_ERROR_RETURN(_sys_usw_acl_map_ip_frag(lchip, pKey->data, &value, &mask));
        }
        value = (CTC_IP_FRAG_YES == pKey->data) ? 0: value;
        mask = (CTC_IP_FRAG_YES == pKey->data) ? 0: mask;
        CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add), ret, error_proc);
        if((is_add && CTC_IP_FRAG_YES == pKey->data)||!(CTC_ACL_KEY_IPV4_EXT == pe->key_type && CTC_EGRESS == pe->group->group_info.dir))
        {
            qid = SYS_ACL_FIELD_IS_FRAG;
            value = (CTC_IP_FRAG_YES == pKey->data && is_add) ? 1: 0;
            mask = (CTC_IP_FRAG_YES == pKey->data && is_add) ? 1: 0;
            CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add), ret, error_proc);
        }
    }
    else
    {
        CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add), ret, error_proc);
    }

    qid = SYS_ACL_FIELD_SRH_COMMON_HDR;
    if(((DRV_IS_TMM(lchip) || DRV_IS_TMG(lchip)) && ACL_KEY_FIELD_IS_SRH(pKey->type))
        && ((is_add && !CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_SRH_COMMON_HDR)) || !sys_tmm_check_srv6_field_is_used(lchip, pe)))
    {
        value = (1<<10)|(p_usw_acl_master[lchip]->srv6_rsv_udf_idx);
        mask  = 0x7FF;
        CTC_ERROR_GOTO(sys_tmm_acl_build_qid_field(lchip, pe, qid,  p_data, p_mask, is_add), ret, error_proc);
        SYS_ACL_SET_FLAG(pe->flag, SYS_ACL_ENTRY_FLAG_SRH_COMMON_HDR, is_add);
    }
    if (pKey->type == CTC_FIELD_KEY_L4_TYPE)
    {
        pKey->data = sys_usw_l4_type_map(lchip, pKey->data, TRUE);
    }

    if (CTC_FIELD_KEY_PORT == pKey->type)
    {
        qid = port_qid;
    }
    else
    {
        qid = pKey->type;
    }

    CTC_ERROR_RETURN(_sys_tmm_acl_set_field_union(lchip, qid, pKey, pe, is_add));

    if (pKey->ext_mask == &ext_mask_temp)
    {
        pKey->ext_mask = NULL;
    }

    return CTC_E_NONE;
error_proc:
    switch(pKey->type)
    {
    case CTC_FIELD_KEY_SVLAN_RANGE:
    case CTC_FIELD_KEY_CVLAN_RANGE:
    case CTC_FIELD_KEY_IP_PKT_LEN_RANGE:
    case CTC_FIELD_KEY_L4_SRC_PORT_RANGE:
    case CTC_FIELD_KEY_L4_DST_PORT_RANGE:
    case CTC_FIELD_KEY_UDF_RANGE:
	case CTC_FIELD_KEY_SUDF_RANGE:
        _sys_tmm_acl_get_range_bitmap(lchip, pe, pKey, FALSE, p_data, p_mask);
        break;
    case CTC_FIELD_KEY_ETHER_TYPE :
        sys_tmm_acl_remove_compress_ether_type(lchip, global_pp_en, dir, pe->un.g4.ether_type_index);
        pe->un.g4.ether_type_index = 0;
        pe->un.g4.ether_type_valid = 0;
        break;
    }
    return ret;
}


int32
_sys_tmm_acl_get_key_field( uint8 lchip,
                            uint32 qid,
                            sys_acl_entry_t* pe,
                            uint32* key_width,
                            uint32* value_array,
                            uint32* mask_array,
                            uint16* offset_dst)
{
    int32 ret = CTC_E_NONE;
    uint16 j = 0;
    uint16 offset_src = 0;
    uint32 width = 0;
    sys_acl_field_offset_info_t offset;
    sys_acl_field_offset_info_t *p_offset = &offset;
    sys_acl_flex_key_t *p_flex_key = pe->ua->buffer.fk;

    sal_memset(&offset, 0, sizeof(offset));
    ret = _sys_tmm_acl_get_offset(lchip, qid, p_flex_key, &offset, &width);

    *key_width = width;

    for (j = 0; j < p_offset->num_offsets; j++)
    {
        width = p_offset->width[j];
        offset_src = p_offset->offset[j];
        CTC_BMP_COPY_RANGE(value_array, *offset_dst, pe->ua->buffer.key,  offset_src,  width);
        CTC_BMP_COPY_RANGE(mask_array,  *offset_dst, pe->ua->buffer.mask, offset_src,  width);
        *offset_dst += width;
    }
    return ret;
}

int32
sys_tmm_acl_get_key_field(uint8 lchip, void* pe_info, ctc_field_key_t* pKey, uint32* key_width)
{
    uint8  loop = 0;
    uint8  is_udf = 0;
    uint8 granularity = 1;
    int32 ret = 0;
    uint32 qid = 0;
    uint16 offset_dst = 0;
    uint32 value_array[9] = {0};
    uint32 mask_array[9] = {0};
    //uint8* p_udf_data = NULL;
    sys_acl_entry_t* pe = NULL;
    sys_acl_flex_key_t* fk = NULL;
    sys_acl_udf_entry_t* p_udf_entry = NULL;

    pe = (sys_acl_entry_t *)pe_info;
    qid = pKey->type;

    fk = pe->ua->buffer.fk;


    //nsh -- use_sys_field = (qid != CTC_FIELD_KEY_IPV6_DA) && (qid != CTC_FIELD_KEY_IPV6_SA) && (qid != CTC_FIELD_KEY_UDF) && (qid != CTC_FIELD_KEY_NSH_CONTEXT_HEADER);
    is_udf = (qid == CTC_FIELD_KEY_UDF) || (qid == CTC_FIELD_KEY_UDF_AD_ID) || (qid == CTC_FIELD_KEY_SUDF) || (qid == CTC_FIELD_KEY_SUDF_AD_ID);

    ret = _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
    if (ret && (qid != CTC_FIELD_KEY_IPV6_DA) && (qid != CTC_FIELD_KEY_IPV6_SA) && (qid != CTC_FIELD_KEY_PORT_BITMAP) && !is_udf && (qid != CTC_FIELD_KEY_XDATA) )
    {
        return 1;
    }

    if ((offset_dst > 32) || (qid == CTC_FIELD_KEY_IPV6_DA) || (qid == CTC_FIELD_KEY_IPV6_SA) || is_udf || (qid == CTC_FIELD_KEY_XDATA) )
    {
        switch(pKey->type)
        {
        case CTC_FIELD_KEY_MAC_DA:
        case CTC_FIELD_KEY_MAC_SA:
        case CTC_FIELD_KEY_SENDER_MAC:
        case CTC_FIELD_KEY_TARGET_MAC:
            {
                SYS_GET_HW_MAC((uint8*)(pKey->ext_data), value_array);
                SYS_GET_HW_MAC((uint8*)(pKey->ext_mask), mask_array);
            }
            break;
        case CTC_FIELD_KEY_IPV6_DA:
            {
                qid = SYS_ACL_FIELD_IPV6_DA_CHUNK0;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_IPV6_DA_CHUNK1;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_IPV6_DA_CHUNK2;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_IPV6_DA_CHUNK3;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
            }
            SYS_GET_HW_IP6((uint32*)(pKey->ext_data), (uint32*)(value_array));
            SYS_GET_HW_IP6((uint32*)(pKey->ext_mask), (uint32*)(mask_array));
            break;
        case CTC_FIELD_KEY_IPV6_SA:
            {
                qid = SYS_ACL_FIELD_IPV6_SA_CHUNK0;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_IPV6_SA_CHUNK1;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_IPV6_SA_CHUNK2;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_IPV6_SA_CHUNK3;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
            }
            SYS_GET_HW_IP6((uint32*)(pKey->ext_data), (uint32*)(value_array));
            SYS_GET_HW_IP6((uint32*)(pKey->ext_mask), (uint32*)(mask_array));
            break;
        case CTC_FIELD_KEY_SUDF:
        case CTC_FIELD_KEY_SUDF_AD_ID:
            _sys_tmm_acl_get_key_field(lchip, SYS_ACL_FIELD_UDF1_PROF_ID, pe, key_width, value_array, mask_array, &offset_dst);
            _sys_usw_acl_get_udf_entry_by_ad_id(lchip, value_array[0], &p_udf_entry);
            granularity = p_udf_entry ? p_udf_entry->granularity : 1;
            offset_dst = 0;
            for (loop=0; loop<CTC_ACL_UDF_FIELD_NUM; loop++)
            {
                if (!CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_UDF1_CHUNK0+loop))
                {
                    offset_dst += 16;
                    continue;
                }
                _sys_tmm_acl_get_key_field(lchip, SYS_ACL_FIELD_UDF1_CHUNK0+loop, pe, key_width, value_array, mask_array, &offset_dst);
            }
            SYS_USW_SET_USR_UDF(granularity, (uint8*)(pKey->ext_data), (uint32*)(value_array));
            SYS_USW_SET_USR_UDF(granularity, (uint8*)(pKey->ext_mask), (uint32*)(mask_array));
            break;
        case CTC_FIELD_KEY_UDF:
        case CTC_FIELD_KEY_UDF_AD_ID:
            _sys_tmm_acl_get_key_field(lchip, SYS_ACL_FIELD_UDF2_PROF_ID, pe, key_width, value_array, mask_array, &offset_dst);
            _sys_usw_acl_get_udf_entry_by_ad_id(lchip, value_array[0], &p_udf_entry);
            granularity = p_udf_entry ? p_udf_entry->granularity : 1;
            offset_dst = 0;
            for (loop=0; loop<CTC_ACL_UDF_FIELD_NUM; loop++)
            {
                if (!CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_UDF2_CHUNK0+loop))
                {
                    offset_dst += 16;
                    continue;
                }
                _sys_tmm_acl_get_key_field(lchip, SYS_ACL_FIELD_UDF2_CHUNK0+loop, pe, key_width, value_array, mask_array, &offset_dst);
            }
            SYS_USW_SET_USR_UDF(granularity, (uint8*)(pKey->ext_data), (uint32*)(value_array));
            SYS_USW_SET_USR_UDF(granularity, (uint8*)(pKey->ext_mask), (uint32*)(mask_array));
            break;
        case CTC_FIELD_KEY_XDATA:
        {
            int8* f_sel = fk->xdata_sel;
            uint8 is_stk = fk->key_type == CTC_ACL_KEY_STK_HDR;
            uint8 width[10] = {32, 32, 16, 16, 16, 16, 8, 8, 8, 8};
            uint8 offset[8] = {0, 32, 0, 16, 32, 40, 48, 56};
            uint32* p_xd = (uint32*)pKey->ext_data;
            uint32* p_xm = (uint32*)pKey->ext_mask;
            uint32  qid_start = SYS_ACL_FIELD_XDATA_DW0;
            uint32  qid_end = SYS_ACL_FIELD_XDATA_B3;

            if(is_stk)
            {
                qid_start   = SYS_ACL_FIELD_STK_XDATA_DW0;
                qid_end     = SYS_ACL_FIELD_STK_XDATA_B3;
            }
            for(qid = qid_start; qid <= qid_end; qid++)
            {
                if (!CTC_ACL_KSET_ISSET(fk->kset, qid))
                {
                    continue;
                }
                loop = qid - qid_start;
                offset_dst = is_stk ? offset[loop] : (width[loop] * f_sel[loop]);
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, p_xd, p_xm, &offset_dst);
            }
            break;
        }
        case CTC_FIELD_KEY_DOT1AE_SCI:
        case CTC_FIELD_KEY_SATPDU_PDU_BYTE:
            {
                ACL_SET_HW_SATPDU_BYTE((uint32*)pKey->ext_data, value_array);
                ACL_SET_HW_SATPDU_BYTE((uint32*)pKey->ext_mask, mask_array);
            }
            break;
#ifdef nsh
        case CTC_FIELD_KEY_NSH_CONTEXT_HEADER:
            {
                qid = SYS_ACL_FIELD_NSH_CH_CHUNK0;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_NSH_CH_CHUNK1;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_NSH_CH_CHUNK2;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
                qid = SYS_ACL_FIELD_NSH_CH_CHUNK3;
                _sys_tmm_acl_get_key_field(lchip, qid, pe, key_width, value_array, mask_array, &offset_dst);
            }
            SYS_ACL_WORD_REVERSE(pKey->ext_data, value_array, 4*sizeof(uint32));
            SYS_ACL_WORD_REVERSE(pKey->ext_mask, mask_array,  4*sizeof(uint32));
            break;
#endif
        default:
            sal_memcpy(pKey->ext_data, value_array, sizeof(value_array));
            sal_memcpy(pKey->ext_mask, mask_array, sizeof(mask_array));
            break;
        }

    }
    else
    {
        if (qid == CTC_FIELD_KEY_ETHER_TYPE)
        {
            if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_FULL_ETHER_TYPE))
            {
                pKey->data = value_array[0];
                pKey->mask = mask_array[0];
            }
            else
            {
                if(pe->un.g4.ether_type_valid)
                {
                    pKey->data = sys_usw_acl_get_ether_type_by_index(lchip, pe, pe->un.g4.ether_type_index);
                    pKey->mask = 0xFFFF;
                }
            }
        }
        else if (qid == CTC_FIELD_KEY_IP_FRAG)
        {
            uint8 value = 0;
            uint8 mask = 0;
            _sys_usw_acl_unmap_ip_frag(lchip, value_array[0], mask_array[0], &value, &mask);
            pKey->data = value;
            pKey->mask = mask;
        }
        else if (qid == SYS_ACL_FIELD_IS_FRAG)
        {
            uint8 value = 0;
            uint8 mask = 0;
            _sys_usw_acl_unmap_ip_frag(lchip, value_array[0], mask_array[0], &value, &mask);
            pKey->data = value;
            pKey->mask = mask;
        }
        else if (qid == CTC_FIELD_KEY_PKT_FWD_TYPE)
        {
            uint8 value = 0;
            sys_usw_acl_unmap_fwd_type(value_array[0],  &value);
            pKey->data = value;
            pKey->mask = mask_array[0] & 0xF;
        }
        else if (qid == CTC_FIELD_KEY_L4_TYPE)
        {
            pKey->data = sys_usw_l4_type_map(lchip, value_array[0], FALSE);
            pKey->mask = mask_array[0];
        }
        else if (qid == CTC_FIELD_KEY_L4_USER_TYPE)
        {
            pKey->data = sys_usw_l4_user_type_map(lchip, value_array[0], FALSE);
            pKey->mask = mask_array[0];
        }
        else if (qid == CTC_FIELD_KEY_INNER_HEAD)
        {
            pKey->data = value_array[0]? 1: 0;
            pKey->mask = mask_array[0];
        }
        else if (qid == CTC_FIELD_KEY_PORT)
        {
            pKey->data = SYS_MAP_DRV_GPORT_TO_CTC_GPORT(value_array[0] & 0xFFFF);
            pKey->mask = mask_array[0] & 0xFFFF;
        }
        else if (qid == CTC_FIELD_KEY_NPM_IM_FLOW_ID && (DRV_IS_TMM(lchip) || DRV_IS_TMG(lchip)))
        {
            pKey->data = value_array[0] & (1<<28) ? (value_array[0] >> 8) & 0xFFFFF : 0;
            pKey->mask = (mask_array[0] >> 8) & 0xFFFFF;
        }
        else if (qid == CTC_FIELD_KEY_SRV6_SRH_FLAG)
        {
            pKey->data = value_array[0] & 0xFFFF;
            pKey->mask = mask_array[0] & 0xFFFF;
        }
        else
        {
            pKey->data = value_array[0];
            pKey->mask = mask_array[0];
        }
    }


    return 0;
}

STATIC void
sys_output_port_bitmap(uint8 lchip, uint32* in_data, uint32* in_mask, uint8 mode)
{
    uint8 width = 8;
    uint8 loop = 0;
    uint8 port_base = 0;

    width = 1 << mode;
    port_base = (in_data[width] & 0xf) << (mode+5);

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Port Bitmap(Base/Value): %d/", port_base);
    do{
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, " 0x%x", ~in_mask[loop]);
    }while(++loop < width);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "\n");
}

char*
sys_output_xdata(uint32* xdata, uint8 is_stk)
{
    static  char out_str[36];
    if(is_stk)
        sal_sprintf(out_str, "0X%08X%08X", xdata[1], xdata[0]);
    else
        sal_sprintf(out_str, "0X%08X%08X%08X%08X", xdata[3], xdata[2], xdata[1], xdata[0]);
    return out_str;
}


int32
sys_tmm_acl_show_key_field(uint8 lchip, sys_acl_entry_t* pe, uint8 show_inner)
{
    uint8  loop = 0;
    uint16 qid = 0;
    sys_acl_flex_key_t *fk = pe->ua->buffer.fk;
    uint32 value[9];
    uint32 mask[9];
    ctc_field_key_t key;
    uint32 key_width = 0;
    int32 ret = 0;
    uint8 list_num = 0;
    uint16 int_qlist[] = {SYS_ACL_FIELD_INT_TYPE,SYS_ACL_FIELD_INT_VER,SYS_ACL_FIELD_INT_HOP_ML,SYS_ACL_FIELD_INT_REMAIN_HOP_CNT,SYS_ACL_FIELD_INT_INSTRUCTION};
    uint16 qid_list[][2] = {{SYS_ACL_FIELD_L4_DP_SHARE, CTC_FIELD_KEY_L4_DST_PORT},
                            {SYS_ACL_FIELD_L4_SP_SHARE, CTC_FIELD_KEY_L4_SRC_PORT},
                            {SYS_ACL_FIELD_VN_ID_SHARE, CTC_FIELD_KEY_VN_ID},
                            {SYS_ACL_FIELD_VXLAN_FLAGS_SHARE, CTC_FIELD_KEY_VN_ID},
                            {SYS_ACL_FIELD_XD_IM_FLOW_ID, CTC_FIELD_KEY_NPM_IM_FLOW_ID},
                            {SYS_ACL_FIELD_XD_IM_L_FLAG,  CTC_FIELD_KEY_NPM_IM_L_FLAG},
                            {SYS_ACL_FIELD_XD_IM_D_FLAG,  CTC_FIELD_KEY_NPM_IM_D_FLAG},
                            {SYS_ACL_FIELD_XD_IM_SRC_D_FLAG, CTC_FIELD_KEY_NPM_IM_D_FLAG},
                            {SYS_ACL_FIELD_IPV6_IM_FLOW_ID, CTC_FIELD_KEY_NPM_IM_FLOW_ID},
                            {SYS_ACL_FIELD_IPV6_IM_L_FLAG,  CTC_FIELD_KEY_NPM_IM_L_FLAG},
                            {SYS_ACL_FIELD_IPV6_IM_D_FLAG,  CTC_FIELD_KEY_NPM_IM_D_FLAG}  };

    list_num = sizeof(qid_list)/sizeof(uint16)/2;
    for(loop = 0; loop < list_num; loop++)
    {
        qid = qid_list[loop][0];
        if (CTC_ACL_KSET_ISSET(fk->kset, qid))
        {
            sal_memset(&key, 0, sizeof(key));
            key.type = qid;
            sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
            if(0 != key.mask)
            {
                if(qid_list[loop][1] == CTC_FIELD_KEY_NPM_IM_FLOW_ID && DRV_FROM_AT(lchip))
                {
                    sys_usw_npm_im_recovery_flow_id(lchip, key.data, &key.data);
                }
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n", ifp_field_str[qid_list[loop][1]], key.data, key.mask);
            }
        }
    }
    /*Special process for port/pbm*/
    qid = SYS_ACL_FIELD_PBM;
    if (CTC_ACL_KSET_ISSET(fk->kset, qid))
    {
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_KEY_SHARE_MODE_PBM == pe->key_port_mode)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Port Bitmap(Base/Value): %d, 0x%x\n",((key.data>>16)&0xF)*16, (~key.mask)&0xFFFF);
        }
    }

    qid = (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_GPORT_SHARE)) ?     SYS_ACL_FIELD_GPORT_SHARE :
          (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_LOGIC_PORT_SHARE)) ? SYS_ACL_FIELD_LOGIC_PORT_SHARE : SYS_ACL_FIELD_COUNT;
    if (qid != SYS_ACL_FIELD_COUNT)
    {
        uint32 dest_map = 0;
        uint32 dest_chipid = 0;
        uint32 dest_id = 0;
        uint8   gport_type = 0;
 
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);

        gport_type =  (key.data >> 16) & 0x3;
        dest_map = key.data & 0xffff;
        dest_chipid = SYS_DECODE_DESTMAP_GCHIP(dest_map);
        dest_id  = SYS_DECODE_DESTMAP_LPORT(dest_map);
        key.data = SYS_MAP_DRV_LPORT_TO_CTC_GPORT(dest_chipid, dest_id);

        if(0 != key.mask && (gport_type ==DRV_ENUM(DRV_FLOWPORTTYPE_GPORT) || gport_type == DRV_ENUM(DRV_FLOWPORTTYPE_LPORT)))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n", ifp_field_str[qid], key.data & 0xFFFF, key.mask & 0xFFFF);
        }
    }

    qid = SYS_ACL_FIELD_PORT_BITMAP_MODE0;
    while(qid <= SYS_ACL_FIELD_PORT_BITMAP_MODE3 && !CTC_ACL_KSET_ISSET(fk->kset, qid))
    {
        qid++;
    }
    if (qid <= SYS_ACL_FIELD_PORT_BITMAP_MODE3)
    {
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(!SYS_ACL_MASK_IS_EMPTY(mask))
        {
            sys_output_port_bitmap(lchip, key.ext_data, key.ext_mask, qid-SYS_ACL_FIELD_PORT_BITMAP_MODE0);
        }
    }

    if (CTC_ACL_KSET_ISSET(fk->kset, CTC_FIELD_KEY_PORT) && CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_GPORT))
    {
        sal_memset(&key, 0, sizeof(key));
        key.type = CTC_FIELD_KEY_PORT;
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                            ifp_field_str[CTC_FIELD_KEY_PORT], key.data & 0xFFFF, key.mask & 0xFFFF);
    }

    if (CTC_ACL_KSET_ISSET(fk->kset, CTC_FIELD_KEY_LOGIC_SRC_PORT) && CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_LOGIC_PORT))
    {
        sal_memset(&key, 0, sizeof(key));
        key.type = CTC_FIELD_KEY_LOGIC_SRC_PORT;
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        qid = pe->group->group_info.dir == CTC_INGRESS ? CTC_FIELD_KEY_LOGIC_SRC_PORT : CTC_FIELD_KEY_LOGIC_DST_PORT;
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                            ifp_field_str[qid], key.data & 0xFFFF, key.mask & 0xFFFF);
    }

    if (CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_FULL_ETHER_TYPE))
    {
        sal_memset(&key, 0, sizeof(key));
        key.type = SYS_ACL_FIELD_FULL_ETHER_TYPE;
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(0 != key.mask)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                                ifp_field_str[CTC_FIELD_KEY_ETHER_TYPE], key.data & 0xFFFF, key.mask & 0xFFFF);
        }
    }

    qid = CTC_FIELD_KEY_IPV6_DA;
    do
    {
        if (!CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK0) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK1) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK2) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_DA_CHUNK3))
        {
            break;
        }
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_MASK_IS_EMPTY(mask))
        {
            break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s/",
                                    ifp_field_str[qid],
                                    sys_output_ipv6(key.ext_data));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n",
                                    sys_output_ipv6(key.ext_mask));
    }
    while(0);

    qid = CTC_FIELD_KEY_IPV6_SA;
    do
    {
        if (!CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK0) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK1) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK2) &&
            !CTC_ACL_KSET_ISSET(fk->kset, SYS_ACL_FIELD_IPV6_SA_CHUNK3))
        {
            break;
        }
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_MASK_IS_EMPTY(mask))
        {
            break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s/",ifp_field_str[qid],sys_output_ipv6(key.ext_data));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n",sys_output_ipv6(key.ext_mask));
    }
    while(0);

    /* CTC_FIELD_KEY_XDATA */
    do
    {
        uint32 xdata[4] = {0};
        uint32 xmask[4] = {0};
        qid = SYS_ACL_FIELD_XDATA_DW0;
        do 
        {
            if(CTC_ACL_KSET_ISSET(fk->kset, qid))
                break;
        }while(qid++ < SYS_ACL_FIELD_STK_XDATA_B3);
        if(qid > SYS_ACL_FIELD_STK_XDATA_B3)
        {
            break;
        }
        sal_memset(&key, 0, sizeof(key));
        sal_memset(&xdata, 0, sizeof(xdata));
        sal_memset(&xmask, 0, sizeof(xmask));
        key.type = CTC_FIELD_KEY_XDATA;
        key.ext_data = xdata;
        key.ext_mask = xmask;
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_MASK_IS_EMPTY(xmask))
        {
            break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s/",ifp_field_str[CTC_FIELD_KEY_XDATA], sys_output_xdata(xdata, pe->key_type == CTC_ACL_KEY_STK_HDR));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n",sys_output_xdata(xmask, pe->key_type == CTC_ACL_KEY_STK_HDR));
        /*for(loop = 0; loop < xdata.num; loop++)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s %s\n", " ", sys_output_xdata(loop, &x_field[loop]));
        }*/
    }while(0);


#ifdef nsh
    qid = CTC_FIELD_KEY_NSH_CONTEXT_HEADER;
    do
    {
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_MASK_IS_EMPTY(mask))
        {
            break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s", ifp_field_str[qid], sys_usw_acl_output_udf(key.ext_data));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s/", sys_usw_acl_output_udf((uint8*)key.ext_data[CTC_ACL_UDF_BYTE_NUM/8]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s", sys_usw_acl_output_udf(key.ext_mask));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n", sys_usw_acl_output_udf((uint8*)key.ext_mask[CTC_ACL_UDF_BYTE_NUM/8]));
    }
    while(0);
#endif

    qid = CTC_FIELD_KEY_SUDF_AD_ID;
    do
    {
        if(!CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_SUDF))  /*0:sudf_id 1:sudf_ad_id*/
        {
            break;
        }
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_MASK_IS_EMPTY(mask))
        {
            break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s", ifp_field_str[qid], sys_usw_acl_output_udf((uint8*)&value[0]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s/", sys_usw_acl_output_udf((uint8*)&value[CTC_ACL_UDF_BYTE_NUM/8]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s", sys_usw_acl_output_udf((uint8*)&mask[0]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n", sys_usw_acl_output_udf((uint8*)&mask[CTC_ACL_UDF_BYTE_NUM/8]));
    }while (0);

    qid = CTC_FIELD_KEY_SUDF;
    do
    {
        if(CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_SUDF))  /*0:sudf_id 1:sudf_ad_id*/
        {
            break;
        }
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_MASK_IS_EMPTY(mask))
        {
            break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s", ifp_field_str[qid], sys_usw_acl_output_udf((uint8*)&value[0]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s/", sys_usw_acl_output_udf((uint8*)&value[CTC_ACL_UDF_BYTE_NUM/8]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s", sys_usw_acl_output_udf((uint8*)&mask[0]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n", sys_usw_acl_output_udf((uint8*)&mask[CTC_ACL_UDF_BYTE_NUM/8]));
    }
    while(0);

    qid = CTC_FIELD_KEY_UDF_AD_ID;
    do
    {
        if(!CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_UDF)) /*0:udf_id 1:udf_ad_id*/
        {
            break;
        }
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_MASK_IS_EMPTY(mask))
        {
            break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s", ifp_field_str[qid], sys_usw_acl_output_udf((uint8*)&value[0]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s/", sys_usw_acl_output_udf((uint8*)&value[CTC_ACL_UDF_BYTE_NUM/8]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s", sys_usw_acl_output_udf((uint8*)&mask[0]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n", sys_usw_acl_output_udf((uint8*)&mask[CTC_ACL_UDF_BYTE_NUM/8]));
    }while(0);

    qid = CTC_FIELD_KEY_UDF;
    do
    {
        if(CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_UDF)) /*0:udf_id 1:udf_ad_id*/
        {
            break;
        }
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(SYS_ACL_MASK_IS_EMPTY(mask))
        {
            break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s", ifp_field_str[qid], sys_usw_acl_output_udf((uint8*)&value[0]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s/", sys_usw_acl_output_udf((uint8*)&value[CTC_ACL_UDF_BYTE_NUM/8]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s", sys_usw_acl_output_udf((uint8*)&mask[0]));
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n", sys_usw_acl_output_udf((uint8*)&mask[CTC_ACL_UDF_BYTE_NUM/8]));
    }
    while(0);
    qid = SYS_ACL_FIELD_IS_FRAG;
    if (CTC_ACL_KSET_ISSET(fk->kset, qid))
    {
        sal_memset(&key, 0, sizeof(key));
        key.type = qid;
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if (key.mask)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                                            ifp_field_str[CTC_FIELD_KEY_IP_FRAG], key.data, key.mask);
        }
    }

    sal_memset(value, 0, sizeof(value));
    sal_memset(mask, 0, sizeof(mask));

    /*CTC field*/
    CTC_BMP__ITER(fk->kset.w, CTC_FIELD_KEY_NUM, qid)
    {

        sal_memset(&key, 0, sizeof(key));
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        key.ext_data = value;
        key.ext_mask = mask;
        key.type = qid;

        if(CTC_E_NONE != _sys_tmm_acl_check_field_union2(lchip, qid, pe))
        {
            continue;
        }

        ret = sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);

        if (ret != 0)
        {
            continue;
        }

        if (key_width < 32)
        {
            if (key.mask == 0)
            {
                continue;
            }

            switch(qid)
            {
            case CTC_FIELD_KEY_SVLAN_RANGE:
            case CTC_FIELD_KEY_CVLAN_RANGE:
            case CTC_FIELD_KEY_IP_PKT_LEN_RANGE:
            case CTC_FIELD_KEY_L4_SRC_PORT_RANGE:
            case CTC_FIELD_KEY_L4_DST_PORT_RANGE:
            case CTC_FIELD_KEY_UDF_RANGE:
            case CTC_FIELD_KEY_SUDF_RANGE:
            {
                uint16 min  = 0;
                uint16 max  = 0;
                ret = _sys_tmm_acl_get_range_value(lchip, pe, qid,  &min, &max);
                if(ret != 0)
                {
                    continue;
                }
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d-%d\n",
                                ifp_field_str[qid], min, max);
            }
                break;
            case CTC_FIELD_KEY_DISCARD:
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                                ifp_field_str[qid], (key.data >> 6)&0x1, (key.mask >> 6)&0x1);

                break;
            case CTC_FIELD_KEY_METADATA:
                if ( SYS_ACL_KEY_SHARE_MODE_METADATA == pe->key_port_mode )
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                                    ifp_field_str[qid], key.data&0x3FFF, key.mask&0x3FFF);
                }
                break;
            case CTC_FIELD_KEY_PORT:
            case CTC_FIELD_KEY_LOGIC_SRC_PORT:
                break;
            case CTC_FIELD_KEY_DST_CID:
            case CTC_FIELD_KEY_SRC_CID:
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n", ifp_field_str[qid], (key.data&0xffff), (key.mask&0xffff));
                break;
            case CTC_FIELD_KEY_STK_DST_GPORT:
             {
                uint32 dst_port = 0;
                uint32 dest_map = 0;
                uint32 dest_chipid = 0;
                uint32 dest_id = 0;

                dest_map = key.data;
                dest_chipid = SYS_DECODE_DESTMAP_GCHIP(dest_map);
                dest_id  = SYS_DECODE_DESTMAP_LPORT(dest_map);
                dst_port = SYS_MAP_DRV_LPORT_TO_CTC_GPORT(dest_chipid, dest_id);

                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : 0x%X/0x%X\n", ifp_field_str[qid], dst_port, key.mask);
             }
                break;
            case CTC_FIELD_KEY_FID_CID:
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                                ifp_field_str[pe->group->group_info.dir ? CTC_FIELD_KEY_LOGIC_SRC_PORT : CTC_FIELD_KEY_FID_CID], key.data, key.mask);
                break;
            default:
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                                ifp_field_str[qid], key.data, key.mask);
            }

        }
        else if(key_width == 32)
        {
            if (key.mask == 0)
            {
                continue;
            }
            if ((qid == CTC_FIELD_KEY_IP_DA) || \
                (qid == CTC_FIELD_KEY_IP_SA) || \
                (qid == CTC_FIELD_KEY_ARP_SENDER_IP) || \
                (qid == CTC_FIELD_KEY_ARP_TARGET_IP))
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s/",
                                    ifp_field_str[qid],
                                    sys_output_ipv4(key.data));
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n",
                                    sys_output_ipv4(key.mask));
            }
            else
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : 0x%X/0x%X\n",
                            ifp_field_str[qid], key.data, key.mask);
            }
        }
        else
        {
            if (SYS_ACL_MASK_IS_EMPTY(mask))
            {
                continue;
            }

            switch(qid)
            {
            case CTC_FIELD_KEY_MAC_SA:
            case CTC_FIELD_KEY_MAC_DA:
            case CTC_FIELD_KEY_SENDER_MAC:
            case CTC_FIELD_KEY_TARGET_MAC:
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s/",
                                    ifp_field_str[qid],
                                    sys_output_mac(key.ext_data));
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n",
                                    sys_output_mac(key.ext_mask));

                }
                break;

            case CTC_FIELD_KEY_IPV6_DA:
            case CTC_FIELD_KEY_IPV6_SA:
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s/",
                                    ifp_field_str[qid],
                                    sys_output_ipv6(key.ext_data));
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n",
                                    sys_output_ipv6(key.ext_mask));

                }
                break;


            case CTC_FIELD_KEY_UDF:
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s/",
                                    ifp_field_str[qid],
                                    sys_usw_acl_output_udf(key.ext_data));
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%s\n",
                                    sys_usw_acl_output_udf(key.ext_mask));

                }
                break;


            case CTC_FIELD_KEY_AWARE_TUNNEL_INFO:
                {
                    if (((value[1]&mask[1])>>30)==3)
                    {
                        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s(%s) : %u/",
                                        ifp_field_str[qid], "GRE_KEY",
                                        value[0]);
                        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "0x%X\n",
                                        mask[0]);
                    }
                    else
                    {
                        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s(%s) : %u/",
                                        ifp_field_str[qid], "VXLAN_VNI",
                                        value[0]);
                        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "0x%X\n",
                                        mask[0]);
                    }
                }
                break;

            case CTC_FIELD_KEY_SATPDU_PDU_BYTE:
            case CTC_FIELD_KEY_DOT1AE_SCI:
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : 0x", ifp_field_str[qid]);
                    for (loop=0; loop<8; loop++)
                    {
                        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%02X", *((uint8*)value+loop));
                    }
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "/0x");
                    for (loop=0; loop<8; loop++)
                    {
                        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%02X", *((uint8*)mask+loop));
                    }
                }
                break;

            default:
                break;
            }
        }

    }

   /*Convert data*/
    qid = CTC_FIELD_KEY_CPU_REASON_ID;
    //if (pe->key_reason_id)
    {   /* excepetion grp_id & falte excpt */
        //SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n", ifp_field_str[qid], pe->key_reason_id, 0xFF);
    }
    qid = CTC_FIELD_KEY_DST_NHID;
    //if (SYS_ACL_AD_TYPE_DISABLE == pe->ad_type && pe->un.normal.key_dst_nhid)
    {   /* dest_map && dsnh_offset */
        //SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %u/0x%X\n", ifp_field_str[qid], pe->un.normal.key_dst_nhid, 0xFFFF);
    }

    qid = CTC_FIELD_KEY_DST_GPORT;
    do
    {
        uint32 dst_port = 0;
        uint32 dest_map = 0;
        uint32 dest_chipid = 0;
        uint32 dest_id = 0;
        sal_memset(&key, 0, sizeof(key));
        key.ext_data = value;
        key.ext_mask = mask;
        sal_memset(value, 0, sizeof(value));
        sal_memset(mask, 0, sizeof(mask));
        key.type = SYS_ACL_FIELD_DESTMAP;
        sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
        if(key.mask == 0)
        {
            break;
        }
        dest_map = key.data;
        dest_chipid = SYS_DECODE_DESTMAP_GCHIP(dest_map);
        dest_id  = SYS_DECODE_DESTMAP_LPORT(dest_map);
        dst_port = SYS_MAP_DRV_LPORT_TO_CTC_GPORT(dest_chipid, dest_id);

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : 0x%X/0x%X\n", ifp_field_str[qid], dst_port, key.mask);
    }
    while(0);

    qid = CTC_FIELD_KEY_IS_Y1731_OAM;
    if (CTC_FLAG_ISSET(pe->flag, SYS_ACL_ENTRY_FLAG_Y1731))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n", ifp_field_str[qid], 1, 1);
    }

    if(1 == p_usw_acl_master[SYS_PP_BASE(lchip)]->ifa_ver)
    {
        list_num = sizeof(int_qlist)/sizeof(uint16);
        for(loop = 0; loop < list_num; loop++)
        {
            qid = int_qlist[loop];
            if (CTC_ACL_KSET_ISSET(fk->kset, qid))
            {
                sal_memset(&key, 0, sizeof(key));
                key.type = qid;
                sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
                if(0 != key.mask)
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : 0x%X/0x%X\n", ifp_field_str[int_qlist[loop]], key.data, key.mask);
                }
            }
        }
    }

    /*SYS field*/
    if (show_inner)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "\n\nInternal field:\n")
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "------------------------------\n")
        BMP_ITER2(fk->kset.w, CTC_FIELD_KEY_NUM, SYS_ACL_FIELD_COUNT,  qid)
        {

            sal_memset(&key, 0, sizeof(key));
            sal_memset(value, 0, sizeof(value));
            sal_memset(mask, 0, sizeof(mask));
            key.ext_data = value;
            key.ext_mask = mask;
            key.type = qid;

            ret = sys_tmm_acl_get_key_field(lchip, pe, &key, &key_width);
            if (ret != 0)
            {
                continue;
            }

            if (key.mask == 0)  /*only show width<=32 inner field, ext_mask not used*/
            {
                continue;
            }

            if (key_width < 32)
            {
                switch(qid)
                {
                case SYS_ACL_FIELD_PBM:
                    break;

                default:

                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d/0x%X\n",
                                    ifp_field_str[qid], key.data, key.mask);
                    break;
                }
            }
            else if(key_width == 32)
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : 0x%X/0x%X\n",
                                                      ifp_field_str[qid], key.data, key.mask);
            }

        }
    }
    return 0;
}

#define SYS_ACL_GET_FIELD_WIDTH(field, kfield_cfg_info, kfield_cfg, chunk_idx, o_width)\
{\
    kfield_cfg = kfield_cfg_info->field_cfg_info[field]->field_cfg_arr;\
    for (chunk_idx=0; chunk_idx<kfield_cfg->num_chunks; chunk_idx++)\
    {\
        o_width += kfield_cfg->e_params[chunk_idx].width;\
    }\
}

void
sys_tmm_acl_show_kset(uint8 lchip, ctc_acl_flex_key_t *acl_flex_key)
{
    uint16 loop = 0;
    uint16 loop1 = 0;
    uint16 chunk_idx = 0;
    uint16 req_bits = 0;
    uint32 width = 0;
    uint8  p_lkup_type = 0;
    sys_acl_kfield_cfg_info_db_t *kfield_cfg_info = NULL;
    sys_acl_kfield_cfg_t *kfield_cfg = NULL;
    sys_acl_flex_key_t* fk = NULL;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-8s  %-24s  %-8s\n", "ID", "FIELD", "WIDTH");
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "----------------------------------------------------\n");
    fk = ctc_vector_get(SYS_ACL_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, acl_flex_key->key_type));
    fk = !fk && acl_flex_key->key_type < CTC_ACL_KEY_FLEX_BASE? ctc_vector_get(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, acl_flex_key->key_type)): fk;
    if (!fk) return;

    kfield_cfg_info = p_usw_acl_master[lchip]->kgn_field_cfg_info_db;
    for (loop = 0; loop < CTC_FIELD_KEY_NUM; loop++)
    {
        if(!CTC_BMP_ISSET(acl_flex_key->kset.w, loop))
        {
            continue;
        }
        width = 0;
        switch(loop)
        {
            case CTC_FIELD_KEY_PORT:
                if(acl_flex_key->port_bitmap_width)
                {
                    if(!CTC_FLAG_ISSET(acl_flex_key->flag, CTC_ACL_FLEX_FLAG_PBM_NO_GPORT))
                    {
                        SYS_ACL_GET_FIELD_WIDTH(CTC_FIELD_KEY_PORT, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                    }
                    chunk_idx = SYS_ACL_FIELD_PORT_BITMAP_MODE0 + acl_flex_key->port_bitmap_width - CTC_ACL_PORT_BITMAP_WIDTH_32;
                    SYS_ACL_GET_FIELD_WIDTH(chunk_idx, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
                else
                {
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_PBM, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
                break;
            case CTC_FIELD_KEY_PORT_BITMAP :
                chunk_idx = SYS_ACL_FIELD_PORT_BITMAP_MODE0 + acl_flex_key->port_bitmap_width - CTC_ACL_PORT_BITMAP_WIDTH_32;
                SYS_ACL_GET_FIELD_WIDTH(chunk_idx, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                break;
            case CTC_FIELD_KEY_CPU_REASON_ID :
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_EXCEPTION_GID, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_EXCEPTION_EXCP_EN, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                if(acl_flex_key->dir == CTC_INGRESS)
                {
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_EXCEPTION_FATAL_EXCP_ID, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_EXCEPTION_FATAL_EN, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
                break;
            case CTC_FIELD_KEY_DST_GPORT:
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_DESTMAP, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_DEST_APS, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_DEST_ECMP, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                break;
            case CTC_FIELD_KEY_DST_NHID :
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_DESTMAP, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_NH_OFFSET, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_DEST_APS, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_DEST_ECMP, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_NH_EXT, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                break;
            case CTC_FIELD_KEY_ETHER_TYPE: 
                if(acl_flex_key->compress_ether_type == FALSE)
                {
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_FULL_ETHER_TYPE, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
                else
                {
                    SYS_ACL_GET_FIELD_WIDTH(CTC_FIELD_KEY_ETHER_TYPE, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
                break;
            case CTC_FIELD_KEY_UDF:
            case CTC_FIELD_KEY_UDF_AD_ID:
                loop1 = 0;
                ctc_bit_range_count((uint32*)&acl_flex_key->udf_bmp, 0, 16, (int32*)&width);
                if (acl_flex_key->udf_bmp & 0xFF)
                {
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_UDF2_PROF_ID, kfield_cfg_info, kfield_cfg, chunk_idx, loop1);
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_UDF2_VALID_BITMAP, kfield_cfg_info, kfield_cfg, chunk_idx, loop1);
                }
                width = (width<<4)+loop1;
                break;
            case CTC_FIELD_KEY_SUDF:
            case CTC_FIELD_KEY_SUDF_AD_ID:
                loop1 = 0;
                ctc_bit_range_count((uint32*)&acl_flex_key->sudf_bmp, 0, 16, (int32*)&width);
                if (acl_flex_key->sudf_bmp & 0xFF)
                {
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_UDF1_PROF_ID, kfield_cfg_info, kfield_cfg, chunk_idx, loop1);
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_UDF1_VALID_BITMAP, kfield_cfg_info, kfield_cfg, chunk_idx, loop1);
                }
                width = (width<<4)+loop1;
                break;
            case CTC_FIELD_KEY_IPV6_DA:
                ctc_bit_range_count((uint32*)&acl_flex_key->ipv6_da_bmp, 0, 4, (int32*)&width);
                width = width<<5;
                break;
            case CTC_FIELD_KEY_IPV6_SA :
                ctc_bit_range_count((uint32*)&acl_flex_key->ipv6_sa_bmp, 0, 4, (int32*)&width);
                width = width<<5;
                break;
            case CTC_FIELD_KEY_XDATA :
            {
                uint8   is_stk = fk->key_type == CTC_ACL_KEY_STK_HDR;
                uint32  qid_start = SYS_ACL_FIELD_XDATA_DW0;
                uint32  qid_end = SYS_ACL_FIELD_XDATA_B3;

                if(is_stk)
                {
                    qid_start   = SYS_ACL_FIELD_STK_XDATA_DW0;
                    qid_end     = SYS_ACL_FIELD_STK_XDATA_B3;
                }
                for(loop1 = qid_start; loop1 <= qid_end; loop1++)
                {
                    if(!CTC_BMP_ISSET(fk->kset.w, loop1))
                        continue;
                    SYS_ACL_GET_FIELD_WIDTH(loop1, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
            }
            break;
            case CTC_FIELD_KEY_STK_HDR_EXIST :
                SYS_ACL_GET_FIELD_WIDTH(CTC_FIELD_KEY_STK_HDR_EXIST, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                if(CTC_BMP_ISSET(fk->kset.w, SYS_ACL_FIELD_STK_HDR_BMP))
                {
                    SYS_ACL_GET_FIELD_WIDTH(SYS_ACL_FIELD_STK_HDR_BMP, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
                break;
            case CTC_FIELD_KEY_NPM_IM_FLOW_ID:
            case CTC_FIELD_KEY_NPM_IM_D_FLAG:
            case CTC_FIELD_KEY_NPM_IM_L_FLAG:
                SYS_ACL_GET_FIELD_WIDTH(loop, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                if(acl_flex_key->dir == CTC_EGRESS)
                {
                    width = width << 1;
                }
                break;
            case CTC_FIELD_KEY_IS_Y1731_OAM :
                SYS_ACL_GET_FIELD_WIDTH(CTC_FIELD_KEY_L4_TYPE, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                SYS_ACL_GET_FIELD_WIDTH(CTC_FIELD_KEY_L4_USER_TYPE, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                break;
            case CTC_FIELD_KEY_INT_TYPE:
            case CTC_FIELD_KEY_INT_VER:
            case CTC_FIELD_KEY_INT_HOP_ML:
            case CTC_FIELD_KEY_INT_REMAIN_HOP_CNT:
            case CTC_FIELD_KEY_INT_INSTRUCTION:
                if(1 == p_usw_acl_master[SYS_PP_BASE(lchip)]->ifa_ver)
                {
                    uint16 qid = loop;
                    qid = (qid == CTC_FIELD_KEY_INT_TYPE) ? SYS_ACL_FIELD_INT_TYPE : ((qid == CTC_FIELD_KEY_INT_VER) ? SYS_ACL_FIELD_INT_VER :
                                (((qid == CTC_FIELD_KEY_INT_HOP_ML) ? SYS_ACL_FIELD_INT_HOP_ML :((qid == CTC_FIELD_KEY_INT_REMAIN_HOP_CNT) ? SYS_ACL_FIELD_INT_REMAIN_HOP_CNT : SYS_ACL_FIELD_INT_INSTRUCTION))));
                    SYS_ACL_GET_FIELD_WIDTH(qid, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
                else
                {
                    SYS_ACL_GET_FIELD_WIDTH(loop, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                }
                break;
            default:
                SYS_ACL_GET_FIELD_WIDTH(loop, kfield_cfg_info, kfield_cfg, chunk_idx, width);
                break;
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-8d  %-24s  %-8d\n", loop, ifp_field_str[loop], width);
    }
    fk = ctc_vector_get(SYS_ACL_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, acl_flex_key->key_type));
    fk = !fk && acl_flex_key->key_type < CTC_ACL_KEY_FLEX_BASE ?
         ctc_vector_get(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, acl_flex_key->key_type)):
         fk;
    if (!fk) return;
    SYS_ACL_KEY_SIZE_TO_PART_NUM(fk->key_size, ACL_SHARE_V(loop1, part_num));
    loop = SYS_ACL_FIELD_SRH_COMMON_HDR;
    if(CTC_BMP_ISSET(fk->kset.w, loop))
    {
        width = 0;
        SYS_ACL_GET_FIELD_WIDTH(loop, kfield_cfg_info, kfield_cfg, chunk_idx, width);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-8d  %-24s  %-8d\n", loop, ifp_field_str[loop], width);
    }
    loop = 0;
    do
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-8d  %-24s  %-8d\n", SYS_ACL_FIELD_LT_ID+loop, ifp_field_str[SYS_ACL_FIELD_LT_ID+loop], 5);
    }while( (++loop) < ACL_SHARE_V(loop1, part_num) );
    req_bits = fk->key_req_bits + fk->key_size * 5;
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "----------------------------------------------------\n");
    switch(acl_flex_key->size)
    {
    case CTC_ACL_KEY_SIZE_160:
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Key Size: 160, used: %u\n", req_bits);
        break;
    case CTC_ACL_KEY_SIZE_320:
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Key Size: 320, used: %u\n", req_bits);
        break;
    case CTC_ACL_KEY_SIZE_480:
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Key Size: 480, used: %u\n", req_bits);
        break;
    default:
        break;
    }
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Alloced Ltid:");
    ACL_SHARE_V(loop1, max_block_num) = acl_flex_key->dir == CTC_INGRESS ? ACL_IGS_BLOCK_MAX_NUM : ACL_EGS_BLOCK_MAX_NUM;
    SYS_ACL_IGS_BLK_LOOP(loop, ACL_SHARE_V(loop1, max_block_num))
    {
        if (CTC_IS_BIT_SET(fk->ltid_valid, loop))
        {
            if (loop && (loop & 7) == 0)
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "\n             ");
            }
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, " (B%02u)%-2u", loop, fk->ltid[loop]);
        }
    }
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "\n");
    if (CTC_FLAG_ISSET(fk->flags, CTC_ACL_FLEX_KEY_FLAG_LKUP_TYPE))
    {
        char* acl_lkup_type[CTC_ACL_TCAM_LKUP_TYPE_MAX] = {
            "l2",       "l2-l3",
            "l3",       "vlan",
            "l3-ext",   "l2-l3-ext",
            "cid",      "interface",
            "forward",  "forward-ext",
            "copp",     "udf",
            "stk-hdr"
        };

        p_lkup_type = sys_usw_unmap_acl_tcam_lkup_type(lchip, fk->lkup_type);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Bind lkup type: %s\n", acl_lkup_type[p_lkup_type]);
    }

}

const char*
sys_tmm_acl_get_key_type_name(uint8 key_type)
{
    if (key_type<CTC_ACL_KEY_FLEX_BASE)
    {
        return key_type_str[key_type];
    }
    else
    {
        sal_sprintf(flex_key_str, "FLEX_KEY(%03d)", key_type);
        return flex_key_str;
    }
}

#if defined(ARCTIC)
int32
sys_at_acl_build_xkey_field(uint8 lchip, uint16 ctc_field, sys_xdata_fk_t* xdata_fk, void* user_data)
{
    sys_acl_xkey_user_data_t* xkey_user_data = ACL_SHARE_PTR(user_data, sys_acl_xkey_user_data_t);
    
    xdata_fk->info.width = xdata_fk->info.share_field = 0;
    switch(ctc_field)
    {
    case CTC_FIELD_KEY_UDF:
    case CTC_FIELD_KEY_SUDF:
    case CTC_FIELD_KEY_UDF_AD_ID:
    case CTC_FIELD_KEY_SUDF_AD_ID:
        /*1. udf valid */
#if 0
        if(0 != (xkey_user_data->udf_data&0xf))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_VALID;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
        if(0 != (xkey_user_data->udf_data&0xf0))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_HI_VALID;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
#endif

        /*2. udf hit idx */
        xdata_fk->info.ptr_field_name = "UDF";
        xdata_fk->info.field = SYS_FK_F_UDF_HIT_INDEX;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));

        /*3. udf data*/
        if (CTC_IS_BIT_SET(xkey_user_data->udf_data, 0))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_DATA0;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
        if (CTC_IS_BIT_SET(xkey_user_data->udf_data, 1))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_DATA1;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
        if (CTC_IS_BIT_SET(xkey_user_data->udf_data, 2))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_DATA2;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
        if (CTC_IS_BIT_SET(xkey_user_data->udf_data, 3))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_DATA3;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
        if (CTC_IS_BIT_SET(xkey_user_data->udf_data, 4))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_DATA4;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
            xdata_fk->info.field = SYS_FK_F_UDF_HI_DATA4;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
        if (CTC_IS_BIT_SET(xkey_user_data->udf_data, 5))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_DATA5;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
            xdata_fk->info.field = SYS_FK_F_UDF_HI_DATA5;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
        if (CTC_IS_BIT_SET(xkey_user_data->udf_data, 6))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_HI_DATA6;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }
        if (CTC_IS_BIT_SET(xkey_user_data->udf_data, 7))
        {
            xdata_fk->info.field = SYS_FK_F_UDF_DATA7;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
        }

        break;
    case CTC_FIELD_KEY_PORT_CID:
    case CTC_FIELD_KEY_XDATA:
        {
            uint8 i = 0;
            ctc_xdata_t* p_xdata = NULL;
            ctc_xdata_field_t* p_field = NULL;

            p_xdata = xkey_user_data->xdata;
            xdata_fk->info.ptr_field_name = "XDATA";
            for(i = 0; i<p_xdata->num; i++)
            {
                p_field = &p_xdata->fields[i];
                xdata_fk->info.field = SYS_FK_F_XDATA;
                xdata_fk->info.src_pos = p_field->offset/CTC_XDATA_WIDTH_4;
                xdata_fk->info.width = p_field->width;
                xdata_fk->info.mask = p_field->mask;
                CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)xdata_fk));
            }
        }
        break;

    default:
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_SUPPORT, "xkey field not support!");
    }
    return CTC_E_NONE;
}


STATIC int32
sys_at_acl_check_flex_hash_uno(uint8 lchip, ctc_acl_flex_key_t* acl_flex_key)
{
    uint16 loop;
    ctc_acl_kset_t kset_base;
    ctc_acl_kset_t* p_kset_user = NULL;
    ctc_acl_kset_t* p_kset = NULL;
    ctc_xdata_local_type_t type;
    ctc_xdata_field_t* p_field = NULL;

    type = acl_flex_key->origin_key_type ? CTC_XDATA_LOCAL_TYPE_FLOW_HASH: CTC_XDATA_LOCAL_TYPE_FLOW_HASH_EXT;
    MCHIP_XDATA(lchip)->local_get_kset(lchip, type, &kset_base);
    p_kset_user = &acl_flex_key->kset;

    if(CTC_XDATA_LOCAL_TYPE_FLOW_HASH == type)
    {
        CTC_VALUE_RANGE_CHECK(acl_flex_key->origin_key_type, CTC_ACL_KEY_HASH_MAC, CTC_ACL_KEY_HASH_NSH);
        CTC_VALUE_RANGE_CHECK(acl_flex_key->mode, CTC_FIELD_FLEX_KEY_MODE_APPEND,CTC_FIELD_FLEX_KEY_MODE_MAX-1);
        p_kset = &p_usw_acl_master[lchip]->support_kset[CTC_BOTH_DIRECTION+acl_flex_key->origin_key_type-CTC_ACL_KEY_HASH_MAC];
        if(CTC_FIELD_FLEX_KEY_MODE_APPEND != acl_flex_key->mode &&(acl_flex_key->origin_key_type == CTC_ACL_KEY_HASH_L2_L3 || 
            acl_flex_key->origin_key_type == CTC_ACL_KEY_HASH_IPV6))/* quad key not support replace and double append */
        {
            return CTC_E_NOT_SUPPORT;
        }
    }
    else
    {
        if(CTC_FIELD_FLEX_KEY_MODE_NONE != acl_flex_key->mode)
        {
            return CTC_E_NOT_SUPPORT;
        }
        for(loop = 0;loop < CTC_FIELD_KEY_NUM; loop++)
        {
            if(!CTC_BMP_ISSET(acl_flex_key->kset.w, loop))
            {
                continue;
            }
            if(loop != CTC_FIELD_KEY_XDATA && loop != CTC_FIELD_KEY_UDF)
            {
                SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_SUPPORT, "only support udf and xdata!");
            }
        }
    }

    for (loop = 0; loop < CTC_B2W_SIZE(CTC_FIELD_KEY_NUM); loop++)
    {
        /* check user kset and support kset */
        if(kset_base.w[loop] != (kset_base.w[loop]|p_kset_user->w[loop]) ||
            (CTC_XDATA_LOCAL_TYPE_FLOW_HASH == type && CTC_FIELD_FLEX_KEY_MODE_REPLACE !=acl_flex_key->mode && (0 != (p_kset->w[loop] & p_kset_user->w[loop]))))
        {
            return CTC_E_INVALID_PARAM;
        }
    }

    /* check xdata width and offset */
    if(CTC_BMP_ISSET(acl_flex_key->kset.w, CTC_FIELD_KEY_XDATA))
    {
        for(loop = 0; loop < acl_flex_key->xdata.num; loop++)
        {
            p_field = &acl_flex_key->xdata.fields[loop];
            switch(p_field->width)
            {
                case CTC_XDATA_WIDTH_4:
                case CTC_XDATA_WIDTH_8:
                case CTC_XDATA_WIDTH_16:
                    if((0 != p_field->offset % p_field->width)||(CTC_CONST128 < p_field->offset + p_field->width))
                    {
                        return CTC_E_INVALID_PARAM;
                    }
                break;
                default:
                    return CTC_E_INVALID_PARAM;
            }
        }
    }

    return CTC_E_NONE;
}

STATIC uint32
_sys_at_acl_map_fk_field_type(uint8 lchip, uint16 field_type, uint8* p_fk_field_type, ctc_acl_flex_key_t* acl_flex_key, uint8* num)
{
    uint8 field_num = 0;
    uint8 loop = 0;

    switch(field_type)
    {
        case CTC_FIELD_KEY_L3_TYPE:
            *p_fk_field_type = SYS_FK_F_PR_L3_TYPE;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_L4_TYPE:
            *p_fk_field_type = SYS_FK_F_PR_L4_TYPE;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_L4_USER_TYPE:
            *p_fk_field_type = SYS_FK_F_PR_L4_USER_TYPE;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_VLAN_NUM:
            *p_fk_field_type = SYS_FK_F_PR_VLAN_NUM;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_IP_FRAG:
            *p_fk_field_type = SYS_FK_F_PR_FRAG_INFO;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_DECAP:
            *p_fk_field_type = SYS_FK_F_IS_DECAP;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_IP_HDR_ERROR:
            *p_fk_field_type = SYS_FK_F_PR_IP_HDR_ERROR;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_DISCARD:
            *p_fk_field_type = SYS_FK_F_DISCARD;
            *(p_fk_field_type+1) = SYS_FK_F_DISCARD_TYPE;
            field_num = 2;
            break;
        case CTC_FIELD_KEY_L2_TYPE:
            *p_fk_field_type = SYS_FK_F_PR_L2_TYPE;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_STP_STATE:
            *p_fk_field_type = SYS_FK_F_STP_STATE;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_CTAG_VALID:
            *p_fk_field_type = SYS_FK_F_CVLAN_ID_VALID;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_STAG_VALID:
            *p_fk_field_type = SYS_FK_F_SVLAN_ID_VALID;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_SVLAN_ID:
            *p_fk_field_type = SYS_FK_F_SVLAN_ID_LO;
            *(p_fk_field_type+1) = SYS_FK_F_SVLAN_ID_HI;
            field_num = 2;
            break;
        case CTC_FIELD_KEY_CVLAN_ID:
            *p_fk_field_type = SYS_FK_F_CVLAN_ID_LO;
            *(p_fk_field_type+1) = SYS_FK_F_CVLAN_ID_HI;
            field_num = 2;
            break;
        case CTC_FIELD_KEY_INTERFACE_ID:
            *p_fk_field_type = SYS_FK_F_L3_IF_ID;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_LOGIC_SRC_PORT:
            *p_fk_field_type = SYS_FK_F_LOGIC_SRC_PORT;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_FID:
            *p_fk_field_type = SYS_FK_F_FID;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_VRFID:
            *p_fk_field_type = SYS_FK_F_VRF_ID;
            field_num = 1;
            break;
        case CTC_FIELD_KEY_XDATA:
            for (loop = 0; loop < acl_flex_key->xdata.num; loop++)
            {
                *(p_fk_field_type + loop) = SYS_FK_F_XDATA;
            }
            field_num = acl_flex_key->xdata.num;
            break;

        default:
            return CTC_E_INVALID_PARAM;
    }
    if (num)
    {
        *num = field_num;
    }
    return CTC_E_NONE;
}


int32
_sys_at_acl_set_flex_hash_key(uint8 lchip, sys_xdata_fk_t* p_fk, ctc_acl_flex_key_t* acl_flex_key,uint8* is_xkey)
{
    int32 ret = CTC_E_NONE;
    uint16 i;
    uint8 j;
    uint8 xpi_field_num = 0;
    uint8 isxkey = 0;
    uint8 fk_field[SYS_XDATA_FK_FIELD_NUM] = {0};

    for(i = 0; i < CTC_FIELD_KEY_NUM; i++)
    {
        if(!CTC_BMP_ISSET(acl_flex_key->kset.w, i))
        {
            continue;
        }
        sal_memset(&p_fk->info,0,sizeof(sys_xdata_fk_field_t));
        if (_sys_at_acl_map_fk_field_type(lchip, i, fk_field, acl_flex_key, &xpi_field_num))
        {
            continue;
        }
        p_fk->info.ptr_field_name = ifp_field_str[i];
        if(0 == xpi_field_num)
        {
            CTC_ERROR_GOTO(CTC_E_INVALID_PARAM, ret, error0);
        }
        for (j = 0; j < xpi_field_num; j++)
        {
            p_fk->info.field = fk_field[j];
            if (CTC_FIELD_KEY_XDATA == i)
            {
                p_fk->info.src_pos = acl_flex_key->xdata.fields[j].offset/CTC_XDATA_WIDTH_4;
                p_fk->info.width = acl_flex_key->xdata.fields[j].width;
                p_fk->info.mask = acl_flex_key->xdata.fields[j].mask;
            }
            CTC_ERROR_GOTO(MCHIP_XDATA(lchip)->add_fk(lchip, p_fk), ret, error0);
        }
        isxkey = 1;
    }
    p_fk->info.field = SYS_FK_F_FIELD_DONE;
    p_fk->info.ptr_field_name = "done";
    CTC_ERROR_GOTO(MCHIP_XDATA(lchip)->add_fk(lchip, p_fk), ret, error0);
    *is_xkey = isxkey;
    return CTC_E_NONE;
error0:
    if(ret != CTC_E_ENTRY_EXIST)
    {
        MCHIP_XDATA(lchip)->remove_fk(lchip, p_fk);
    }
    return ret;
}


int32
sys_at_acl_set_flex_hash_fields(uint8 lchip, ctc_acl_flex_key_t* acl_flex_key)
{
    //int32 ret = 0;
    sys_acl_flex_hash_t* p_flex_hash;
    sys_xdata_fk_t xdata_fk;
    ctc_xdata_t* p_xdata = NULL;
    uint8 is_xkey = 0;
    uint8  max_udf_bmp = (1<<CTC_ACL_UDF_FIELD_NUM)-1;
    uint8  kset_empty = 0;
    uint16 loop = 0;
    uint32 entry_size = 0;

    if(!DRV_FROM_AT(lchip))
    {
        return CTC_E_NOT_SUPPORT;
    }
    CTC_MAX_VALUE_CHECK(acl_flex_key->size, CTC_ACL_KEY_SIZE_480);
    CTC_MAX_VALUE_CHECK(acl_flex_key->udf_bmp, max_udf_bmp);
    CTC_MAX_VALUE_CHECK(acl_flex_key->sudf_bmp, max_udf_bmp);
    CTC_MIN_VALUE_CHECK(acl_flex_key->key_type, CTC_ACL_KEY_FLEX_BASE);

    kset_empty = sys_usw_acl_array_is_empty((uint8*)acl_flex_key->kset.w, CTC_B2W_SIZE(CTC_FIELD_KEY_NUM)<<2);
    p_flex_hash = ctc_vector_get(SYS_ACL_FLEX_KEY_VEC(lchip),
                                 SYS_ACL_FLEX_KEY_IDX(CTC_INGRESS, acl_flex_key->key_type));
    if (NULL == p_flex_hash && FALSE == kset_empty)   /*set*/
    {
        sal_memset(&xdata_fk, 0, sizeof(sys_xdata_fk_t));
        CTC_ERROR_RETURN(sys_at_acl_check_flex_hash_uno(lchip, acl_flex_key));
        if(acl_flex_key->origin_key_type)
        {
            sys_acl_entry_t pe;
            sys_acl_group_t group;
            uint32 key_id = 0;
            sal_memset(&pe, 0, sizeof(sys_acl_entry_t));
            sal_memset(&group, 0, sizeof(sys_acl_group_t));
            group.group_info.block_id = 0;
            group.group_info.dir = CTC_INGRESS;
            pe.ad_type = SYS_ACL_ACTION_HASH_INGRESS;
            pe.key_type = acl_flex_key->origin_key_type;
            pe.group = &group;
            _sys_usw_acl_get_table_id(lchip, &pe, &key_id, NULL, NULL);
            xdata_fk.tbl_id = key_id;
            xdata_fk.hash_type= 0;
            xdata_fk.type = CTC_XDATA_LOCAL_TYPE_FLOW_HASH;
            xdata_fk.mode = acl_flex_key->mode-1;
            CTC_ERROR_RETURN(_sys_at_acl_set_flex_hash_key(lchip, &xdata_fk, acl_flex_key, &is_xkey));
            CTC_ERROR_DUMP(sys_usw_ftm_query_table_entry_size(lchip, key_id, &entry_size));
            
        }
        else
        {
           entry_size = 12;
           if(CTC_BMP_ISSET(acl_flex_key->kset.w, CTC_FIELD_KEY_XDATA))
           {
               MALLOC_ZERO(MEM_ACL_MODULE, p_xdata, sizeof(ctc_xdata_t)+ (acl_flex_key->xdata.num*sizeof(ctc_xdata_field_t)));
               if(NULL == p_xdata)
               {
                   CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
               }
               sal_memcpy(p_xdata,&acl_flex_key->xdata,sizeof(ctc_xdata_t));
               p_xdata->fields = (void*)p_xdata + sizeof(ctc_xdata_t);
               sal_memcpy(p_xdata->fields,&acl_flex_key->xdata.fields,acl_flex_key->xdata.num*sizeof(ctc_xdata_field_t));
           }
           else if(CTC_BMP_ISSET(acl_flex_key->kset.w, CTC_FIELD_KEY_PORT_CID))
           {
               MALLOC_ZERO(MEM_ACL_MODULE, p_xdata, sizeof(ctc_xdata_t)+ sizeof(ctc_xdata_field_t));
               if(NULL == p_xdata)
               {
                   CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
               }
               p_xdata->fields = (void*)p_xdata + sizeof(ctc_xdata_t);
               p_xdata->num = 1;
               p_xdata->type = CTC_XDATA_LOCAL_TYPE_FLOW_HASH_EXT;
               p_xdata->fields[0].width = CTC_XDATA_WIDTH_4;
               p_xdata->fields[0].offset = SYS_XDATA_PORT_CID_POS;
           }
        }

        MALLOC_ZERO(MEM_ACL_MODULE, p_flex_hash, SYS_ACL_HASH_FK_SIZE(ACL_HASH_MAX_NUM));
        if (NULL == p_flex_hash)
        {
            mem_free(p_xdata);
            CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
        }
        p_flex_hash->entry_count = (void*)p_flex_hash + sizeof(sys_acl_flex_hash_t);
        p_flex_hash->key_type = acl_flex_key->key_type;
        p_flex_hash->key_size = (CTC_FIELD_FLEX_KEY_MODE_DOUBLE_APPEND == acl_flex_key->mode) ? (entry_size/12)*2 : (entry_size/12);
        p_flex_hash->origin_key_type = acl_flex_key->origin_key_type;
        p_flex_hash->node_type = SYS_ACL_FLEX_KEY_NODE_HASH;
        p_flex_hash->mode = acl_flex_key->mode;
        p_flex_hash->udf_bmp = acl_flex_key->udf_bmp;
        p_flex_hash->xdata = p_xdata;

        sal_memcpy(&p_flex_hash->kset, &acl_flex_key->kset, sizeof(ctc_acl_kset_t));

        SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);
        if (FALSE == ctc_vector_add(SYS_ACL_FLEX_KEY_VEC(lchip),
                                    SYS_ACL_FLEX_KEY_IDX(CTC_INGRESS, acl_flex_key->key_type),
                                    p_flex_hash))
        {
            mem_free(p_xdata);
            mem_free(p_flex_hash);
            SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NO_RESOURCE, "set flex hash!");
        }

    }
    else if (NULL != p_flex_hash && TRUE == kset_empty)   /*unset*/
    {
        if(!SYS_ACL_FK_IS_HASH(p_flex_hash))
        {
            return CTC_E_INVALID_CONFIG;
        }
        if (p_flex_hash->presel_cnt)
        {
            CTC_ERROR_RETURN(CTC_E_IN_USE);
        }
        for (loop=0; loop<ACL_HASH_MAX_NUM; loop++)
        {
            if (p_flex_hash->entry_count[loop])
            {
                CTC_ERROR_RETURN(CTC_E_IN_USE);
            }
        }
        if(p_flex_hash->origin_key_type)
        {
            sys_acl_entry_t pe;
            sys_acl_group_t group;
            uint32 key_id = 0;
            sal_memset(&pe, 0, sizeof(sys_acl_entry_t));
            sal_memset(&group, 0, sizeof(sys_acl_group_t));
            group.group_info.block_id = 0;
            group.group_info.dir = CTC_INGRESS;
            pe.ad_type = SYS_ACL_ACTION_HASH_INGRESS;
            pe.key_type = p_flex_hash->origin_key_type;
            pe.group = &group;
            _sys_usw_acl_get_table_id(lchip, &pe, &key_id, NULL, NULL);
            xdata_fk.tbl_id = key_id;
            xdata_fk.hash_type= 0;
            xdata_fk.type = CTC_XDATA_LOCAL_TYPE_FLOW_HASH;
            xdata_fk.mode = p_flex_hash->mode-1;
            MCHIP_XDATA(lchip)->remove_fk(lchip, &xdata_fk);
        }

        SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);
        ctc_vector_del(SYS_ACL_FLEX_KEY_VEC(lchip),
                       SYS_ACL_FLEX_KEY_IDX(CTC_INGRESS, p_flex_hash->key_type));
        mem_free(p_flex_hash->xdata);
        mem_free(p_flex_hash);
    }
    else if (NULL == p_flex_hash && TRUE == kset_empty)    /*do nothing*/
    {
        return CTC_E_NONE;
    }
    else /*(NULL != p_flex_hash && FALSE == kset_empty) update*/
    {
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_SUPPORT, "update flex hash!");
    }
    return CTC_E_NONE;
}

#endif


int32
sys_tmm_acl_set_flex_key_fields(uint8 lchip, ctc_acl_flex_key_t* acl_flex_key)
{
    uint8  size_mode = 0;
    uint8  ltid = 0;
    uint8  key_type = 0;
    uint8  is_empty = 0;
    uint8  loop = 0;
    uint8  part_count = 0;
    uint8  max_block_num = ACL_IGS_BLOCK_MAX_NUM;
    uint16 max_udf_bmp = (1<<CTC_ACL_UDF_FIELD_NUM)-1;
    int32  ret = CTC_E_NONE;
    sys_acl_flex_key_t *fk = NULL;
    sys_acl_flex_key_t *def_fk = NULL;
    ctc_acl_kset_t zero;
    ctc_acl_flex_key_t tmp_acl_flex_key;
    IpeFwdAclCtl2_m fwd_acl_ctl;
    uint32 cmd = 0;
    uint32 tbl_id = 0;
    uint32 field_id = 0;
    uint8  map_idx = 0;

    loop = 0;
    do {
        if (acl_flex_key->kset.w[loop] & (~p_usw_acl_master[lchip]->support_kset[acl_flex_key->dir].w[loop]))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "  Some field is not support in kset!!! \n");
            return CTC_E_NOT_SUPPORT;
        }
    }while(++loop < CTC_B2W_SIZE(CTC_FIELD_KEY_NUM));
    if ((CTC_ACL_KSET_ISSET(acl_flex_key->kset, CTC_FIELD_KEY_MAC_DA) && CTC_ACL_KSET_ISSET(acl_flex_key->kset, CTC_FIELD_KEY_TARGET_MAC)) ||
        (CTC_ACL_KSET_ISSET(acl_flex_key->kset, CTC_FIELD_KEY_MAC_SA) && CTC_ACL_KSET_ISSET(acl_flex_key->kset, CTC_FIELD_KEY_SENDER_MAC)))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "  Mac Da/Sa can't be used with target/sender Mac!!! \n");
        return CTC_E_INVALID_PARAM;
    }
    if(1 == p_usw_acl_master[SYS_PP_BASE(lchip)]->ifa_ver && (CTC_ACL_KSET_ISSET(acl_flex_key->kset, CTC_FIELD_KEY_INT_LEN) || 
        CTC_ACL_KSET_ISSET(acl_flex_key->kset, CTC_FIELD_KEY_INT_MBIT) || CTC_ACL_KSET_ISSET(acl_flex_key->kset, CTC_FIELD_KEY_INT_EBIT)))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, " IFA1.0 not support match int-len int-mbit int-ebit \n");
        return CTC_E_NOT_SUPPORT;
    }
    CTC_MAX_VALUE_CHECK(acl_flex_key->size, CTC_ACL_KEY_SIZE_480);
    CTC_MAX_VALUE_CHECK(acl_flex_key->ipv6_da_bmp, 0xF);
    CTC_MAX_VALUE_CHECK(acl_flex_key->ipv6_sa_bmp, 0xF);
    //nsh -- CTC_MAX_VALUE_CHECK(acl_flex_key->nsh_ch_bmp, 0xF);

    CTC_MAX_VALUE_CHECK(acl_flex_key->port_bitmap_width, CTC_ACL_PORT_BITMAP_WIDTH_MAX-1);
    CTC_MAX_VALUE_CHECK(acl_flex_key->udf_bmp, max_udf_bmp);
    CTC_MAX_VALUE_CHECK(acl_flex_key->sudf_bmp, max_udf_bmp);
    if (CTC_FLAG_ISSET(acl_flex_key->flag, CTC_ACL_FLEX_KEY_FLAG_LKUP_TYPE))
    {
        CTC_MAX_VALUE_CHECK(acl_flex_key->lkup_type, CTC_ACL_TCAM_LKUP_TYPE_MAX-1);
        if (acl_flex_key->l3_type != CTC_PARSER_L3_TYPE_IPV6 && acl_flex_key->l3_type != CTC_PARSER_L3_TYPE_NONE)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Lookup type map only support ipv6 or none!!!\n");
            return CTC_E_INVALID_CONFIG;
        }
    }

    if (acl_flex_key->dir == CTC_EGRESS)
    {
        max_block_num = ACL_EGS_BLOCK_MAX_NUM;
    }

    sal_memcpy(&tmp_acl_flex_key, acl_flex_key, sizeof(ctc_acl_flex_key_t));
    key_type = acl_flex_key->key_type;
    tmp_acl_flex_key.ipv6_da_bmp = acl_flex_key->ipv6_da_bmp == 0? 0xF: acl_flex_key->ipv6_da_bmp;
    tmp_acl_flex_key.ipv6_sa_bmp = acl_flex_key->ipv6_sa_bmp == 0? 0xF: acl_flex_key->ipv6_sa_bmp;
    //nsh -- acl_flex_key->nsh_ch_bmp = acl_flex_key->nsh_ch_bmp == 0? 0xF: acl_flex_key->nsh_ch_bmp;
    tmp_acl_flex_key.udf_bmp = acl_flex_key->udf_bmp == 0 ? max_udf_bmp : acl_flex_key->udf_bmp;
    tmp_acl_flex_key.sudf_bmp = acl_flex_key->sudf_bmp == 0 ? max_udf_bmp : acl_flex_key->sudf_bmp;

    fk = ctc_vector_get(SYS_ACL_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, key_type));
    if(fk && SYS_ACL_FK_IS_HASH(fk))
    {
        return CTC_E_INVALID_CONFIG;
    }
    sal_memset(&zero, 0, sizeof(ctc_acl_kset_t));
    is_empty = (0 == sal_memcmp(&acl_flex_key->kset, &zero, sizeof(ctc_acl_kset_t)));
    if ((NULL != fk) && (1 != is_empty))   /*update*/
    {
        CTC_ERROR_RETURN(CTC_E_ENTRY_EXIST);
    }
    else if ((NULL == fk) && (1 == is_empty))   /*do nothing*/
    {
        return CTC_E_NONE;
    }
    else if ((NULL == fk) && (1 != is_empty))   /*set*/
    {
        if (SYS_ACL_KEY_IS_RSVD(key_type))
        {
            def_fk = ctc_vector_get(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, key_type));
            if (!def_fk)
            {
                return CTC_E_NOT_SUPPORT;
            }
            SYS_ACL_IGS_BLK_LOOP(loop, max_block_num)
            {
                if (def_fk->entry_count[loop])
                {
                    CTC_ERROR_RETURN(CTC_E_IN_USE);
                }
            }
            CTC_ERROR_RETURN(_sys_tmm_acl_map_key_type_to_ltid(key_type, &ltid));
        }
        MALLOC_ZERO(MEM_ACL_MODULE, fk, SYS_ACL_FK_SIZE(max_block_num,sys_acl_flex_key_t));
        if (NULL == fk)
        {
            CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
        }
        fk->entry_count = (void*)fk + sizeof(sys_acl_flex_key_t);
        SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);
        if (FALSE == ctc_vector_add(SYS_ACL_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, key_type), fk))
        {
            CTC_ERROR_GOTO(CTC_E_NO_RESOURCE, ret, error0);
        }
        fk->key_type = key_type;
        fk->node_type = (acl_flex_key->dir == CTC_INGRESS)?(SYS_ACL_FLEX_KEY_NODE_I_TCAM):(SYS_ACL_FLEX_KEY_NODE_E_TCAM);
        fk->flags = acl_flex_key->flag;
        if (SYS_ACL_KEY_IS_RSVD(key_type))
        {
            for (loop=0; loop<max_block_num; loop++)
            {
                fk->ltid[loop] = ltid;
                CTC_BIT_SET(fk->ltid_valid, loop);
            }
        }
        sal_memcpy(&fk->kset, &acl_flex_key->kset, sizeof(ctc_acl_kset_t));
        _sys_tmm_acl_flex_key_update_kset(lchip, fk, &tmp_acl_flex_key);
        switch(acl_flex_key->size)
        {
        case CTC_ACL_KEY_SIZE_160:
            size_mode = SYS_ACL_FK_SIZE_MODE_SINGLE;
            break;
        case CTC_ACL_KEY_SIZE_320:
            size_mode = SYS_ACL_FK_SIZE_MODE_DOUBLE;
            break;
        case CTC_ACL_KEY_SIZE_480:
            size_mode = SYS_ACL_FK_SIZE_MODE_TRIPLE;
            break;
        default:
            size_mode = SYS_ACL_FK_SIZE_MODE_AUTO;
            break;
        }

        SYS_ACL_PKSET_REMOVE(&fk->kset, CTC_FIELD_KEY_PORT_BITMAP);
        if (CTC_FLAG_ISSET(acl_flex_key->flag, CTC_ACL_FLEX_KEY_FLAG_LKUP_TYPE))
        {
            /*1.alloc ltid*/
            fk->lkup_type = sys_usw_map_acl_tcam_lkup_type(lchip, acl_flex_key->lkup_type);
            CTC_ERROR_GOTO(_sys_tmm_acl_fk_alloc_glb_ltid(lchip, acl_flex_key->dir, fk), ret, error1);
            CTC_ERROR_GOTO(_sys_tmm_acl_build_extract_db(lchip, size_mode, fk), ret, error2);
            /*2.set table*/
            if (acl_flex_key->l3_type == CTC_PARSER_L3_TYPE_IPV6)
            {
                CTC_BIT_SET(map_idx, 4);
                CTC_SET_FLAG(fk->flags, SYS_ACL_FK_FLAG_IS_IPV6);
            }
            map_idx |= fk->lkup_type & 0xF;
            tbl_id = sys_at_acl_get_glb_table(lchip, acl_flex_key->dir, SYS_ACL_GLB_TABLE_TYPE_FWD_CTL);
            cmd = DRV_IOR(tbl_id, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &fwd_acl_ctl));
            field_id = acl_flex_key->dir == CTC_INGRESS
                        ? IpeFwdAclCtl2_gLookupTypeMapLtid_0_forceLtidMappingEn_f
                        : EpeAclQosCtl2_gLookupTypeMapLtid_0_forceLtidMappingEn_f;
            DRV_SET_FIELD_V(lchip, tbl_id, field_id+map_idx, &fwd_acl_ctl, 1);
            field_id = acl_flex_key->dir == CTC_INGRESS
                        ? IpeFwdAclCtl2_gLookupTypeMapLtid_0_ltidIndex_f
                        : EpeAclQosCtl2_gLookupTypeMapLtid_0_ltidIndex_f;
            DRV_SET_FIELD_V(lchip, tbl_id, field_id+map_idx, &fwd_acl_ctl, fk->ltid[0]);
            cmd = DRV_IOW(tbl_id, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &fwd_acl_ctl));
        }
        else
        {
            CTC_ERROR_GOTO(_sys_tmm_acl_build_extract_db(lchip, size_mode, fk), ret, error1);
        }
        if (CTC_ACL_KSET_ISSET(acl_flex_key->kset, CTC_FIELD_KEY_PORT_BITMAP))
        {
            SYS_ACL_PKSET_ADD(&fk->kset, CTC_FIELD_KEY_PORT_BITMAP);
        }
    }
    else    /*unset*/
    {
        if (fk->presel_cnt)
        {
            CTC_ERROR_RETURN(CTC_E_IN_USE);
        }
        for (loop=0; loop<max_block_num; loop++)
        {
            if (fk->entry_count[loop])
            {
                CTC_ERROR_RETURN(CTC_E_IN_USE);
            }
        }
        SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);
        if (CTC_FLAG_ISSET(fk->flags, CTC_ACL_FLEX_KEY_FLAG_LKUP_TYPE))
        {
            /*1.unset table*/
            if (CTC_FLAG_ISSET(fk->flags, SYS_ACL_FK_FLAG_IS_IPV6))
            {
                CTC_BIT_SET(map_idx, 4);
            }
            map_idx |= fk->lkup_type & 0xF;
            tbl_id = sys_at_acl_get_glb_table(lchip, acl_flex_key->dir, SYS_ACL_GLB_TABLE_TYPE_FWD_CTL);
            cmd = DRV_IOR(tbl_id, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &fwd_acl_ctl));
            field_id = acl_flex_key->dir == CTC_INGRESS
                        ? IpeFwdAclCtl2_gLookupTypeMapLtid_0_forceLtidMappingEn_f
                        : EpeAclQosCtl2_gLookupTypeMapLtid_0_forceLtidMappingEn_f;
            DRV_SET_FIELD_V(lchip, tbl_id, field_id+map_idx, &fwd_acl_ctl, 0);
            cmd = DRV_IOW(tbl_id, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &fwd_acl_ctl));
        }
        /*2.free ltid*/
        _sys_tmm_acl_fk_free_glb_ltid(lchip, acl_flex_key->dir, fk);

        part_count = fk->key_size;
        SYS_ACL_PART_LOOP(loop, part_count)
        {
            mem_free(fk->field_arr[loop].qid_arr);
            mem_free(fk->field_arr[loop].offset_arr);
        }
        ctc_vector_del(SYS_ACL_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, key_type));
        mem_free(fk);
    }


    return CTC_E_NONE;

error2:
    if (CTC_FLAG_ISSET(acl_flex_key->flag, CTC_ACL_FLEX_KEY_FLAG_LKUP_TYPE))
    {
        _sys_tmm_acl_fk_free_glb_ltid(lchip, acl_flex_key->dir, fk);
    }
error1:
    ctc_vector_del(SYS_ACL_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, key_type));
error0:
    mem_free(fk);
    return ret;
}

int32
sys_tmm_acl_get_flex_key_fields(uint8 lchip, ctc_acl_flex_key_t* acl_flex_key)
{
    sys_acl_flex_key_t *fk = NULL;

    fk = ctc_vector_get(SYS_ACL_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, acl_flex_key->key_type));
    if ((NULL == fk) && SYS_ACL_KEY_IS_RSVD(acl_flex_key->key_type))
    {
        fk = ctc_vector_get(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(acl_flex_key->dir, acl_flex_key->key_type));
    }
    if (NULL == fk)
    {
        CTC_ERROR_RETURN(CTC_E_ENTRY_NOT_EXIST);
    }

    if (SYS_ACL_FK_IS_HASH(fk))
    {
        sal_memcpy(&acl_flex_key->kset, &ACL_SHARE_PTR(fk, sys_acl_flex_hash_t)->kset, sizeof(ctc_acl_kset_t));
        CTC_SET_FLAG(acl_flex_key->flag, CTC_ACL_FLEX_KEY_FLAG_HASH);
    }
    else
    {
        _sys_tmm_acl_flex_key_unmap_kset(lchip, &fk->kset, acl_flex_key);
        acl_flex_key->size = sys_tmm_acl_mapping_fpa2key_size(fk->key_size);
        if (fk->lkup_type)
        {
            acl_flex_key->lkup_type = sys_usw_unmap_acl_tcam_lkup_type(lchip, fk->lkup_type);
            CTC_SET_FLAG(acl_flex_key->flag, CTC_ACL_FLEX_KEY_FLAG_LKUP_TYPE);
        }
        CTC_UNSET_FLAG(acl_flex_key->flag, CTC_ACL_FLEX_KEY_FLAG_HASH);
    }
    return CTC_E_NONE;
}

#define __PRESEL__

int32
sys_tmm_acl_resume_presel_tcam_hw(uint8 lchip, uint8 acl_pri, sys_acl_presel_t* presel)
{
    uint32 cmd = 0;
    uint32 tbl_id = 0;
    uint16 tcam_index = 0;
    tbl_entry_t temp_entry;

    MALLOC_ZERO(MEM_ACL_MODULE, presel->buffer, sizeof(sys_acl_buffer_t));
    if (NULL == presel->buffer)
    {
        return CTC_E_NO_MEMORY;
    }
    temp_entry.data_entry = presel->buffer->key;
    temp_entry.mask_entry = presel->buffer->mask;
    tbl_id = (CTC_INGRESS == presel->dir ? DsLtidSelectTcam_t : DsEgrLtidSelectTcam_t);
    cmd = DRV_IOR(tbl_id, DRV_ENTRY_FLAG);
    SYS_ACL_ENCODE_PRESEL_TCAM_INDEX(acl_pri, presel->fpae.offset_a, tcam_index);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, tcam_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &temp_entry));
    return CTC_E_NONE;
}

//key:DsLtidSelectTcam_t,ad:DsLtidSelectAd0,profile:DsProgramKeyGenProfile0
int32
sys_tmm_acl_op_presel_hw(uint8 lchip, uint8 dst_block_id, uint8 op_mode, uint32 index, sys_acl_presel_temp_entry_t* p_temp_entry, uint8 src_block_id, uint8 global_pp_en)
{
    uint8  is_install = 0;
    uint8  is_reserved = 0;
    uint8  block_id = 0;
    uint8  dir = CTC_INGRESS;
    uint8  acl_prio = 0;
    uint8  src_acl_prio = src_block_id;
    uint8  dst_acl_prio = dst_block_id;
    uint32 cmd = 0;
    uint32 hw_index = 0;
    DsLtidSelectAd_m            ad;
    DsProgramKeyGenProfile_m    profile;
    DsLtidSelectTcam_m          key, mask;
    tbl_entry_t entry;
    void* p_ad = &ad;
    void* p_profile = &profile;
    sys_acl_lt_block_t* p_ltb = NULL;
    sys_acl_block_t*    p_acb = NULL;
    uint32 ad_table[CTC_BOTH_DIRECTION] = {DsLtidSelectAd0_t, DsEgrLtidSelectAd0_t};
    uint32 profile_table[CTC_BOTH_DIRECTION] = {DsProgramKeyGenProfile0_t, DsEgrProgramKeyGenProfile0_t};
    uint32 key_table[CTC_BOTH_DIRECTION] = {DsLtidSelectTcam_t, DsEgrLtidSelectTcam_t};

    acl_prio = block_id = op_mode == SYS_ACL_PRESEL_OP_MODE_COPY? src_block_id: dst_block_id;
    if (block_id>=ACL_IGS_BLOCK_MAX_NUM)
    {
        acl_prio = block_id-ACL_IGS_BLOCK_MAX_NUM;
        dir = CTC_EGRESS;
        src_acl_prio = src_block_id-ACL_IGS_BLOCK_MAX_NUM;
        dst_acl_prio = dst_block_id-ACL_IGS_BLOCK_MAX_NUM;
    }

    p_ltb = &p_usw_acl_master[lchip]->lt_block[block_id];
    p_acb = &p_usw_acl_master[lchip]->block[block_id];
    is_install = CTC_IS_BIT_SET(p_ltb->entry_valid, index);
    is_reserved = (index>=SYS_ACL_KEY_LTID_RSV_BASE && index<=SYS_ACL_KEY_LTID_RSV_MAX) || (p_temp_entry && !p_temp_entry->key.data_entry) ? 1 : 0;

    if  ((is_install && op_mode == SYS_ACL_PRESEL_OP_MODE_ADD)
       ||(!is_install && (op_mode == SYS_ACL_PRESEL_OP_MODE_COPY)))
    {
        return CTC_E_NONE;
    }

    cmd = DRV_IOR(ad_table[dir]+acl_prio, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, index, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), p_ad));
    if (op_mode == SYS_ACL_PRESEL_OP_MODE_COPY && p_acb->fpab.part_num > SYS_ACL_ABS(dst_block_id, src_block_id) && GetDsLtidSelectAd(V, keyLenType_f, p_ad))
    {
        return CTC_E_NONE;
    }

    switch(op_mode)
    {
        case SYS_ACL_PRESEL_OP_MODE_ADD:
        {
            CTC_PTR_VALID_CHECK(p_temp_entry);
            p_ad = p_temp_entry->ad;
            p_profile = p_temp_entry->profile;
            entry.data_entry = p_temp_entry->key.data_entry;
            entry.mask_entry = p_temp_entry->key.mask_entry;
            break;
        }
        case SYS_ACL_PRESEL_OP_MODE_COPY:
        {
            entry.data_entry = (void*)&key;
            entry.mask_entry = (void*)&mask;
            hw_index = index;
            cmd = DRV_IOR(ad_table[dir]+src_acl_prio, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, hw_index, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), p_ad));
            cmd = DRV_IOR(profile_table[dir]+src_acl_prio, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, hw_index, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), p_profile));
            if (!is_reserved)
            {
                SYS_ACL_ENCODE_PRESEL_TCAM_INDEX(src_acl_prio, index, hw_index);
                cmd = DRV_IOR(key_table[dir], DRV_ENTRY_FLAG);
                CTC_ERROR_RETURN(DRV_IOCTL(lchip, hw_index, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &entry));
            }
            break;
        }
        case SYS_ACL_PRESEL_OP_MODE_DEL:
        {
            sal_memset(p_ad, 0, sizeof(DsLtidSelectAd_m));
            sal_memset(p_profile, 0, sizeof(DsProgramKeyGenProfile_m));
            /* need reserve ltid position */
            SetDsProgramKeyGenProfile(V, gL0Extractor8b_0_selFieldId_f + SYS_ACL_EXT_L1_E8_MAX - 1, p_profile, 0xa);
            SetDsProgramKeyGenProfile(V, gL2Extractor1b_0_selFieldId_f + SYS_ACL_EXT_L3_E1_MAX - 1, p_profile, 0x9c);
            SetDsProgramKeyGenProfile(V, gL2Extractor4b_0_selFieldId_f + SYS_ACL_EXT_L3_E4_MAX - 1, p_profile, 0x26);
            break;
        }
        default:
            break;
    }
    hw_index = index;
    cmd = DRV_IOW(ad_table[dir]+dst_acl_prio, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, hw_index, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), p_ad));
    cmd = DRV_IOW(profile_table[dir]+dst_acl_prio, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, hw_index, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), p_profile));

    if (!is_reserved)
    {
        SYS_ACL_ENCODE_PRESEL_TCAM_INDEX(dst_acl_prio, index, hw_index);
        cmd = op_mode == SYS_ACL_PRESEL_OP_MODE_DEL? DRV_IOD(key_table[dir], DRV_ENTRY_FLAG): DRV_IOW(key_table[dir], DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, hw_index, DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !global_pp_en)), &entry));
    }
    p_ltb = &p_usw_acl_master[lchip]->lt_block[dst_block_id];
    if (op_mode == SYS_ACL_PRESEL_OP_MODE_DEL)
    {
        CTC_BIT_UNSET(p_ltb->entry_valid, index);
    }
    else
    {
        CTC_BIT_SET(p_ltb->entry_valid, index);
    }
    return CTC_E_NONE;
}

/*
    no need speed
    no need consider block
*/
int32
sys_tmm_acl_presel_with_league(uint8 lchip, uint8 block_id, uint32 lkup_level_bitmap, uint8 size, uint8 is_set, uint8 global_pp_en)
{
    uint8 block_index = 0, part = 0;
    uint8 league_size = 0;
    uint8 src_block = 0;
    uint8 max_block_num = SYS_ACL_BLOCK_MAX_NUM(block_id);
    uint8 acl_prio = SYS_ACL_BLOCK_PRIO(block_id);
    uint8 op_mode = is_set ? SYS_ACL_PRESEL_OP_MODE_COPY : SYS_ACL_PRESEL_OP_MODE_DEL;
    uint8 block_base = SYS_ACL_BLOCK_BASE(block_id);
    uint16 index = 0;
    sys_acl_lt_block_t* p_ltb = &p_usw_acl_master[lchip]->lt_block[block_id];
    sys_acl_block_t*    p_acb = &p_usw_acl_master[lchip]->block[block_id];
    ctc_fpa_block_t* p_lt_fpab = p_usw_acl_master[lchip]->glb_presel_en ? (&p_usw_acl_master[lchip]->lt_block[block_base].fpab) : (&p_ltb->fpab);

    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);
    while(block_index <= max_block_num)
    {
        if (!CTC_IS_BIT_SET(lkup_level_bitmap, block_index))
        {
            block_index++;
            continue;
        }
        if (block_index == acl_prio)
        {
            block_index += size;
            continue;
        }
        for (index=0; index<SYS_ACL_PRESEL_TCAM_DEPTH; index++)
        {
            SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_ltb->entry_valid, index));
            SYS_ACL_IF_CONTINUE(index < SYS_ACL_KEY_LTID_RSV_BASE && p_lt_fpab->entries[index] == NULL);
            league_size = (CTC_IS_BIT_SET(p_ltb->is_key160, index) || index >= SYS_ACL_KEY_LTID_RSV_BASE) ? p_acb->fpab.part_num : p_lt_fpab->entries[index]->key_size;
            SYS_ACL_IF_CONTINUE(league_size > size);
            SYS_ACL_PART_LOOP(part, league_size)
            {
                src_block = CTC_IS_BIT_SET(p_ltb->is_key160, index) ? block_id : block_id + part;
                sys_tmm_acl_op_presel_hw(lchip, block_index+block_base+part, op_mode, index, NULL, src_block, global_pp_en);
            }
        }
        block_index += size;
    }
    return CTC_E_NONE;
}

/*
    need consider league
    need consider speed
    no consider egress
*/
int32
sys_tmm_acl_presel_with_block(uint8 lchip, uint8 block_id, uint8 part_num, uint8 global_pp_en)
{
    uint8 mode = 0;
    uint8 part = 0, block_index = 0;
    uint8 block_base = SYS_ACL_BLOCK_BASE(block_id);
    uint16 index = 0;
    sys_acl_block_t* p_acb = &p_usw_acl_master[lchip]->block[block_id];
    sys_acl_league_t* p_league = &p_usw_acl_master[lchip]->league[block_id];
    sys_acl_lt_block_t* p_ltb = &p_usw_acl_master[lchip]->lt_block[block_id];
    uint8 max_block_num = SYS_ACL_BLOCK_MAX_NUM(block_id);

    if (part_num == p_acb->fpab.part_num)
    {
        return CTC_E_NONE;
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);
    mode = (part_num > p_acb->fpab.part_num ? 2 : 0) | (p_acb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST ? 1 : 0);
    switch(mode)
    {
    case 0:/*unmerge compress*/
    {
        block_index = block_id;
        for (index=0; index<SYS_ACL_PRESEL_TCAM_DEPTH; index++)
        {
            SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_ltb->is_key160, index));
            for(part=part_num; part<p_acb->fpab.part_num; part++)
            {
                sys_tmm_acl_op_presel_hw(lchip, block_index+part, SYS_ACL_PRESEL_OP_MODE_DEL, index, NULL, block_base, global_pp_en);
            }
        }
        break;
    }
    case 1:/*merged compress*/
    {
        SYS_ACL_IGS_BLK_LOOP(block_index, max_block_num)
        {
            SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, block_index));
            for (index=0; index<SYS_ACL_PRESEL_TCAM_DEPTH; index++)
            {
                SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_ltb->is_key160, index));
                for(part=part_num; part<p_acb->fpab.part_num; part++)
                {
                    sys_tmm_acl_op_presel_hw(lchip, block_index+block_base+part, SYS_ACL_PRESEL_OP_MODE_DEL, index, NULL, block_base, global_pp_en);
                }
            }
        }
        break;
    }
    case 2:/*unmerge expand*/
    {
        block_index = block_id;
        for (index=0; index<SYS_ACL_PRESEL_TCAM_DEPTH; index++)
        {
            SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_ltb->is_key160, index));
            for(part=p_acb->fpab.part_num; part<part_num; part++)
            {
                sys_tmm_acl_op_presel_hw(lchip, block_index+part, SYS_ACL_PRESEL_OP_MODE_COPY, index, NULL, block_id, global_pp_en);
            }
        }
        break;
    }
    default: /*case 3:merged expand*/
    {
        SYS_ACL_IGS_BLK_LOOP(block_index, max_block_num)
        {
            SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, block_index));
            for (index=0; index<SYS_ACL_PRESEL_TCAM_DEPTH; index++)
            {
                SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_ltb->is_key160, index));
                for(part=p_acb->fpab.part_num; part<part_num; part++)
                {
                    sys_tmm_acl_op_presel_hw(lchip, block_index+block_base+part, SYS_ACL_PRESEL_OP_MODE_COPY, index, NULL, block_id, global_pp_en);
                }
            }
        }
        break;
    }
    }
    return CTC_E_NONE;
}

/*
    1.add entry and install pre lkup presel will call
    2.need consider league and expand
*/
int32
sys_tmm_acl_entry_add_presel(uint8 lchip, uint8 block_id, sys_acl_flex_key_t* fk)
{
    uint8 presel_index = 0, part = 0, block_index = 0;
    uint8 league_size = 0;
    uint8 acl_prio = SYS_ACL_BLOCK_PRIO(block_id);
    uint8 first_block = SYS_ACL_BLOCK_BASE(block_id);
    uint8 max_block_num = SYS_ACL_BLOCK_MAX_NUM(block_id);
    uint16 key_type = fk->key_type;
    uint8 dir            = SYS_ACL_BLOCK_DIR(block_id);
    uint8 global_pp_en = SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, dir, (block_id-first_block));
    DsProgramKeyGenProfile_m profile;
    DsLtidSelectAd_m ad;
    sys_acl_presel_temp_entry_t temp_entry;
    sys_acl_block_t* p_acb = &p_usw_acl_master[lchip]->block[block_id];
    sys_acl_league_t* p_league = &p_usw_acl_master[lchip]->league[block_id];
    
    
    if (SYS_ACL_KEY_IS_FLEX(key_type) && !p_usw_acl_master[lchip]->glb_presel_en && !CTC_FLAG_ISSET(fk->flags, CTC_ACL_FLEX_KEY_FLAG_LKUP_TYPE))
    {
        return CTC_E_NONE;
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);
    league_size = SYS_ACL_GET_LEAGUE_SIZE(block_id);
    sal_memset(&ad, 0, sizeof(DsLtidSelectAd_m));
    sal_memset(&profile, 0, sizeof(DsProgramKeyGenProfile_m));
    if (p_usw_acl_master[lchip]->glb_presel_en && SYS_ACL_KEY_IS_FLEX(key_type))
    {
        for (presel_index=0; presel_index<SYS_ACL_PRESEL_NUM; presel_index++)
        {
            if(fk->presel_ent_arr[presel_index] == NULL)
            {
                continue;
            }

            fk->ad_index = fk->presel_ent_arr[presel_index]->fpae.offset_a;
            temp_entry.key.data_entry = (uint32*)fk->presel_ent_arr[presel_index]->buffer->key;
            temp_entry.key.mask_entry = (uint32*)fk->presel_ent_arr[presel_index]->buffer->mask;
            SYS_ACL_PART_LOOP(part, fk->key_size)
            {
                _sys_tmm_acl_fk_build_presel_ad(lchip, part, fk->ltid[acl_prio], fk, &ad);
                _sys_tmm_acl_fk_build_kgen_profile(lchip, part, fk, &profile);
                temp_entry.ad = (void*)&ad;
                temp_entry.profile = (void*)&profile;
                sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_ADD, fk->ad_index, &temp_entry, first_block, global_pp_en);
            }
            if (fk->key_size == 1 && p_acb->fpab.part_num > 1)
            {
                for (part=1;part<p_acb->fpab.part_num; part++)
                {
                    sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_COPY, fk->ad_index, NULL, block_id, global_pp_en);
                }
            }
            if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST)
            {
                SYS_ACL_IGS_BLK_LOOP(block_index, max_block_num)
                {
                    SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, block_index));
                    SYS_ACL_PART_LOOP(part, league_size)
                    {
                        SYS_ACL_IF_CONTINUE(block_index == acl_prio && part == 0);
                        sys_tmm_acl_op_presel_hw(lchip, block_index+first_block+part, SYS_ACL_PRESEL_OP_MODE_COPY, fk->ad_index, NULL, block_id+part, global_pp_en);
                    }
                }
            }
            if (fk->key_size == 1)
            {
                CTC_BIT_SET(p_usw_acl_master[lchip]->lt_block[block_id].is_key160, fk->ad_index);
            }
            fk->presel_ent_arr[presel_index]->fpae.flag = FPA_ENTRY_FLAG_INSTALLED;
        }
    }
    else
    {
        fk->ad_index = fk->ltid[0];
        temp_entry.key.data_entry = NULL;
        temp_entry.key.mask_entry = NULL;
        SYS_ACL_PART_LOOP(part, fk->key_size)
        {
            _sys_tmm_acl_fk_build_presel_ad(lchip, part, fk->ltid[0], fk, &ad);
            _sys_tmm_acl_fk_build_kgen_profile(lchip, part, fk, &profile);
            temp_entry.ad = (void*)&ad;
            temp_entry.profile = (void*)&profile;
            sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_ADD, fk->ad_index, &temp_entry, first_block, global_pp_en);
        }
        if (fk->key_size == 1 && p_acb->fpab.part_num > 1)
        {
            for (part=1; part<p_acb->fpab.part_num; part++)
            {
                sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_COPY, fk->ad_index, NULL, block_id, global_pp_en);
            }
        }
        if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST)
        {
            SYS_ACL_IGS_BLK_LOOP(block_index, max_block_num)
            {
                SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, block_index));
                SYS_ACL_PART_LOOP(part, league_size)
                {
                    SYS_ACL_IF_CONTINUE(block_index == acl_prio && part == 0);
                    sys_tmm_acl_op_presel_hw(lchip, block_index+first_block+part, SYS_ACL_PRESEL_OP_MODE_COPY, fk->ad_index, NULL, block_id + part, global_pp_en);
                }
            }
        }
        if (fk->key_size == 1)
        {
            CTC_BIT_SET(p_usw_acl_master[lchip]->lt_block[block_id].is_key160, fk->ad_index);
        }
    }
    return CTC_E_NONE;
}

int32
sys_tmm_acl_entry_remove_presel(uint8 lchip, uint8 block_id, sys_acl_flex_key_t* fk)
{
    uint8 presel_index = 0, block_index = 0, part = 0;
    uint8 league_size = 0;
    uint8 block_base = SYS_ACL_BLOCK_BASE(block_id);
    uint8 max_block_num = SYS_ACL_BLOCK_MAX_NUM(block_id);
    uint16 ad_index = 0;
    uint16 key_type = fk->key_type;
    sys_acl_block_t* p_acb = &p_usw_acl_master[lchip]->block[block_id];
    sys_acl_league_t* p_league = &p_usw_acl_master[lchip]->league[block_id];

    if (SYS_ACL_KEY_IS_FLEX(key_type) && !p_usw_acl_master[lchip]->glb_presel_en && !CTC_FLAG_ISSET(fk->flags, CTC_ACL_FLEX_KEY_FLAG_LKUP_TYPE))
    {
        return CTC_E_NONE;
    }

    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);
    if (p_usw_acl_master[lchip]->glb_presel_en && SYS_ACL_KEY_IS_FLEX(key_type))
    {
        for (presel_index=0; presel_index<SYS_ACL_PRESEL_NUM; presel_index++)
        {
            if(fk->presel_ent_arr[presel_index] == NULL)
            {
                continue;
            }
            
            ad_index = fk->presel_ent_arr[presel_index]->fpae.offset_a;
            if (fk->key_size == 1)
            {
                CTC_BIT_UNSET(p_usw_acl_master[lchip]->lt_block[block_id].is_key160, ad_index);
            }
            if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST)
            {
                league_size = p_acb->fpab.part_num;
                SYS_ACL_IGS_BLK_LOOP(block_index, max_block_num)
                {
                    SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, block_index));
                    SYS_ACL_PART_LOOP(part, league_size)
                    {
                        sys_tmm_acl_op_presel_hw(lchip, block_index+block_base+part, SYS_ACL_PRESEL_OP_MODE_DEL, ad_index, NULL, block_base, 0);
                    }
                }
            }
            else if (fk->key_size == 1 && p_acb->fpab.part_num > 1)
            {
                SYS_ACL_PART_LOOP(part, p_acb->fpab.part_num)
                {
                    sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_DEL, ad_index, NULL, block_base, 0);
                }
            }
            else
            {
                SYS_ACL_PART_LOOP(part, fk->key_size)
                {
                    sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_DEL, ad_index, NULL, block_base, 0);
                }
            }
            SYS_ACL_PART_LOOP(part, p_acb->fpab.part_num)
            {
                CTC_BIT_UNSET(p_usw_acl_master[lchip]->lt_block[block_id].entry_valid, ad_index);
            }
            fk->presel_ent_arr[presel_index]->fpae.flag = FPA_ENTRY_FLAG_UNINSTALLED;
        }
    }
    else
    {
        if (fk->key_size == 1)
        {
            CTC_BIT_UNSET(p_usw_acl_master[lchip]->lt_block[block_id].is_key160, fk->ltid[0]);
        }
        if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST)
        {
            league_size = p_acb->fpab.part_num;
            SYS_ACL_IGS_BLK_LOOP(block_index, max_block_num)
            {
                SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, block_index));
                SYS_ACL_PART_LOOP(part, league_size)
                {
                    sys_tmm_acl_op_presel_hw(lchip, block_index+block_base+part, SYS_ACL_PRESEL_OP_MODE_DEL, fk->ltid[0], NULL, block_base, 0);
                }
            }
        }
        else if (fk->key_size == 1 && p_acb->fpab.part_num > 1)
        {
            SYS_ACL_PART_LOOP(part, p_acb->fpab.part_num)
            {
                sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_DEL, fk->ltid[0], NULL, block_base, 0);
            }
        }
        else
        {
            SYS_ACL_PART_LOOP(part, fk->key_size)
            {
                sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_DEL, fk->ltid[0], NULL, block_base, 0);
            }
        }
        SYS_ACL_PART_LOOP(part, p_acb->fpab.part_num)
        {
            CTC_BIT_UNSET(p_usw_acl_master[lchip]->lt_block[block_id].entry_valid, fk->ltid[0]);
        }
    }
    return CTC_E_NONE;
}

int32
sys_tmm_acl_add_presel_tcam(uint8 lchip, sys_acl_presel_t *presel)
{
    uint8   key_size = 0;
    uint8   step     = 1;
    uint32  block_index = 0;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    sys_tmm_acl_dump_sys_presel(presel);

    /* alloc block index. */
    CTC_ERROR_RETURN(fpa_usw_alloc_offset(p_usw_acl_master[lchip]->presel_fpa,
                                            &presel->pltb->fpab,           key_size,
                                            presel->fpae.priority,  &block_index));
    /* add to block */
    presel->pltb->fpab.entries[block_index] = &presel->fpae;
    /* set block index */
    presel->fpae.offset_a = block_index;
    presel->fpae.step = step;
    presel->fpae.flag = FPA_ENTRY_FLAG_UNINSTALLED;
    presel->fpae.entry_id = presel->presel_id;
    presel->fpae.key_size = presel->fk->key_size;
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "presel tcam[block%02d] alloc index:%d\n", presel->pltb->block_id, block_index);
    return CTC_E_NONE;
}

int32
sys_tmm_acl_remove_presel_tcam(uint8 lchip, sys_acl_presel_t *presel)
{
    uint32  block_index = 0;

    block_index = presel->fpae.offset_a;
    presel->fpae.offset_a = 0;
    presel->fpae.step = 0;
    presel->fpae.flag = 0;
    fpa_usw_free_offset(&presel->pltb->fpab, block_index);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "presel tcam[block%02d] free index:%d\n", presel->pltb->block_id, block_index);
    return CTC_E_NONE;
}

int32
sys_at_acl_add_presel_fields(uint8 lchip, sys_acl_presel_t* presel, uint8 fields_cnt, ctc_field_key_t *fields, uint8 is_add)
{
    int32  ret = CTC_E_NONE;
#if defined(ARCTIC)
    ParserUdfCamResult_m udf_cam_ad[SYS_UDF_CAM_TYPE_NUM];
    sys_xdata_fk_t xdata_fk;
    sys_acl_xkey_user_data_t xkey_user_data;
    sys_acl_udf_entry_t* p_udf_entry = NULL;
    sys_acl_flex_hash_t* fk = (void*)presel->fk;
    uint16 loop1 = 0;
    uint8  loop = 0;
    uint8  value = is_add ? 1 : 0;
    uint16 udf_ad_id = 0;

    CTC_MAX_VALUE_CHECK(presel->lkup_level, 3);
    sal_memset(&xdata_fk, 0, sizeof(sys_xdata_fk_t));
    for (loop=0; loop<fields_cnt; loop++)
    {
        xdata_fk.type = CTC_XDATA_LOCAL_TYPE_FLOW_HASH_EXT;
        xdata_fk.tbl_id = DsXLookup0HashKey_t + presel->lkup_level - 1;
        xdata_fk.hash_type = ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->hash_type;
        switch(fields[loop].type)
        {
        case CTC_FIELD_KEY_UDF:
            if(!CTC_BMP_ISSET(ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->kset.w, fields[loop].type))
            {
                SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_SUPPORT, "only support udf!");
            }

            if (is_add)
            {
                CTC_PTR_VALID_CHECK(fields[loop].ext_data);
                if(presel->udf_en)
                {
                    SYS_ACL_DUMP_ERROR_RETURN(CTC_E_ENTRY_EXIST, "not support update!");
                }
                _sys_usw_acl_get_udf_info(lchip, ACL_SHARE_PTR(fields[loop].ext_data, ctc_acl_udf_t)->udf_id, &p_udf_entry);
                if (!p_udf_entry || !p_udf_entry->key_index_used)
                {
                    SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_EXIST, "udf entry not exist!");
                }
                CTC_MAX_VALUE_CHECK(p_udf_entry->udf_hit_index, SYS_ACL_XLOOKUP_UDF_HIT_IDX_MAX);

                if(ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->hash_type == p_udf_entry->udf_hit_index)
                {
                    return CTC_E_NONE;
                }

                /*build xkey*/
                xdata_fk.mode = 1;
                xkey_user_data.xdata = fk->xdata;
                xkey_user_data.udf_data = fk->udf_bmp;
                xdata_fk.hash_type = p_udf_entry->udf_hit_index;
                loop1 = 0;
                do {
                    if (!CTC_BMP_ISSET(fk->kset.w, loop1))
                    {
                        continue;
                    }
                    CTC_ERROR_GOTO(sys_at_acl_build_xkey_field(lchip, loop1, &xdata_fk, &xkey_user_data), ret, error0);
                }while(++loop1 < CTC_FIELD_KEY_NUM);
                ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->hash_type = p_udf_entry->udf_hit_index;
                presel->udf_en = 1;
            }
            else
            {
                if(!presel->udf_en)/* do nothing */
                {
                    return CTC_E_NONE;
                }
                CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->remove_fk(lchip, (void*)&xdata_fk));
                udf_ad_id = ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->hash_type;
                _sys_usw_acl_get_udf_entry_by_ad_id(lchip, udf_ad_id, &p_udf_entry);
                if (!p_udf_entry || !p_udf_entry->key_index_used)
                {
                    SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_EXIST, "udf entry not exist!");
                }
                ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->hash_type = 0;
                presel->udf_en = 0;
            }
            /* rewrite udf ad */
            sys_usw_parser_io_get_parser_udf_cam_result(lchip, udf_cam_ad, p_udf_entry->key_index,
                                                        p_udf_entry->is_sudf, p_usw_acl_master[lchip]->udf_mode);
            SetParserUdfCamResult(V, xLookupUdfEn_f, &udf_cam_ad[SYS_UDF_CAM_TYPE_FIB], value);
            SetParserUdfCamResult(V, xLookupUdfEn_f, &udf_cam_ad[SYS_UDF_CAM_TYPE_ACL], value);
            SetParserUdfCamResult(V, xLookupUdfEn_f, &udf_cam_ad[SYS_UDF_CAM_TYPE_SCL], value);
            sys_usw_parser_io_set_parser_udf_cam_result(lchip, udf_cam_ad, p_udf_entry->key_index,
                                                        p_udf_entry->is_sudf, p_usw_acl_master[lchip]->udf_mode);
            break;
        case CTC_FIELD_KEY_PRESEL_ENTRY_VALID:
            if (is_add)
            {
                if (!fields[loop].data)
                {
                    CTC_UNSET_FLAG(presel->fpae.flag, FPA_ENTRY_FLAG_INSTALLED);
                }
                else
                {
                    if(!presel->udf_en)
                    {
                        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_READY, "udf must be config!");
                    }
                    udf_ad_id = ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->hash_type;
                    _sys_usw_acl_get_udf_entry_by_ad_id(lchip, udf_ad_id, &p_udf_entry);
                    if (!p_udf_entry || !p_udf_entry->key_index_used)
                    {
                        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_EXIST, "udf entry not exist!");
                    }
                    xdata_fk.info.field = SYS_FK_F_FIELD_DONE;
                    CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->add_fk(lchip, (void*)&xdata_fk));
                    ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->hash_type = p_udf_entry->udf_hit_index;
                    CTC_SET_FLAG(presel->fpae.flag, FPA_ENTRY_FLAG_INSTALLED);
                }
            }
            break;
        default:
            break;
        }
    }
    return CTC_E_NONE;
error0:
    MCHIP_XDATA(lchip)->remove_fk(lchip, (void*)&xdata_fk);
#endif
    return ret;
}

int32
sys_at_acl_create_hash_presel(uint8 lchip, ctc_acl_presel_t* acl_presel, sys_acl_flex_key_t* fk)
{
    int32 ret = CTC_E_NONE;
    sys_acl_presel_t *presel = NULL;
    if (!CTC_FLAG_ISSET(acl_presel->flag, CTC_ACL_PRESEL_FLAG_LKUP_LEVEL))
    {
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_INVALID_CONFIG, "Flex Hash key must assign lkup level!!!");
    }
    CTC_MAX_VALUE_CHECK(acl_presel->lkup_level, ACL_HASH_MAX_NUM - 1);
    if ((!DRV_TABLE_MAX_INDEX(lchip, DsXLookup1HashKey_t) && acl_presel->lkup_level == 2) || 
    (!DRV_TABLE_MAX_INDEX(lchip, DsXLookup0HashKey_t) && acl_presel->lkup_level == 1))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "%s never alloed by ftm!\n", acl_presel->lkup_level == 1 ? "DsXLookup0HashKey" : "DsXLookup1HashKey");
        return CTC_E_NO_RESOURCE;
    }
    MALLOC_ZERO(MEM_ACL_MODULE, presel, sizeof(sys_acl_presel_t));
    if (NULL == presel)
    {
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NO_MEMORY, "");
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL, 1);
    presel->fk = fk;
    presel->presel_id  = acl_presel->presel_id;
    presel->lkup_level = acl_presel->lkup_level;
    presel->flag = acl_presel->flag;
    presel->dir  = acl_presel->dir;
    if (FALSE == ctc_vector_add(SYS_ACL_PRESEL_VEC(lchip), acl_presel->presel_id, presel))
    {
        CTC_ERROR_GOTO(CTC_E_NO_RESOURCE, ret, error0);
    }
    ACL_SHARE_PTR(fk, sys_acl_flex_hash_t)->presel_cnt++;
    return CTC_E_NONE;
error0:
    mem_free(presel);
    return ret;
}

int32
sys_at_acl_destroy_hash_presel(uint8 lchip, sys_acl_presel_t* presel)
{
    uint8 loop = 0;
    ctc_field_key_t fields;

    SYS_ACL_IGS_BLK_LOOP(loop, ACL_HASH_MAX_NUM)
    {
        if (ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->entry_count[loop])
        {
            CTC_ERROR_RETURN(CTC_E_IN_USE);
        }
    }

    if(presel->udf_en)
    {
        sal_memset(&fields, 0, sizeof(ctc_field_key_t));
        fields.type = CTC_FIELD_KEY_XDATA;
        sys_at_acl_add_presel_fields(lchip, presel, 1, &fields, FALSE);
    }

    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL, 1);
    ACL_SHARE_PTR(presel->fk, sys_acl_flex_hash_t)->presel_cnt--;
    ctc_vector_del(SYS_ACL_PRESEL_VEC(lchip), presel->presel_id);
    mem_free(presel);
    return CTC_E_NONE;
}

int32
sys_tmm_acl_create_presel(uint8 lchip, ctc_acl_presel_t* acl_presel)
{
    int32  ret = CTC_E_NONE;
    uint8  loop = 0/*, loop1 = 0*/;
    uint8  block_id = 0;
    uint8  max_block_num = (acl_presel->dir==CTC_INGRESS)?(ACL_IGS_BLOCK_MAX_NUM):(ACL_EGS_BLOCK_MAX_NUM);
    uint8  block_base = (acl_presel->dir==CTC_INGRESS)?(0):(ACL_IGS_BLOCK_MAX_NUM);
    sys_acl_flex_key_t *fk = NULL;
    sys_acl_presel_t *presel = NULL;
    sys_acl_buffer_t *buffer = NULL;

    presel = ctc_vector_get(SYS_ACL_PRESEL_VEC(lchip), acl_presel->presel_id);
    if (NULL != presel)
    {
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_EXIST, "presel exist!");
    }
    fk = sys_tmm_acl_get_flex_key(lchip, acl_presel->dir, acl_presel->key_type);
    if (NULL == fk)
    {
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_EXIST, "flex key not exist!");
    }

    if (SYS_ACL_FK_IS_HASH(fk))
    {
        return sys_at_acl_create_hash_presel(lchip, acl_presel, (void*)fk);
    }

    if (p_usw_acl_master[lchip]->glb_presel_en)
    {
        if (CTC_FLAG_ISSET(acl_presel->flag, CTC_ACL_PRESEL_FLAG_LKUP_LEVEL))
        {
            SYS_ACL_DUMP_ERROR_RETURN(CTC_E_INVALID_PARAM, "glb presel with lkup level! \n");
        }
    }
    else
    {
        if (!CTC_FLAG_ISSET(acl_presel->flag, CTC_ACL_PRESEL_FLAG_LKUP_LEVEL))
        {
            SYS_ACL_DUMP_ERROR_RETURN(CTC_E_INVALID_PARAM, "per lookup presel with no lkup level! \n");
        }
        CTC_MAX_VALUE_CHECK(acl_presel->lkup_level, max_block_num);
        if ((acl_presel->lkup_level + fk->key_size > max_block_num))
        {
            SYS_ACL_DUMP_ERROR_RETURN(CTC_E_INVALID_PARAM, "Invalid lkup level");
        }
    }

    MALLOC_ZERO(MEM_ACL_MODULE, presel, sizeof(sys_acl_presel_t));
    MALLOC_ZERO(MEM_ACL_MODULE, buffer, sizeof(sys_acl_buffer_t));
    if (NULL == presel || NULL == buffer)
    {
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NO_MEMORY, "");
    }
    presel->buffer = buffer;
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL, 1);
    presel->fk = fk;
    presel->presel_id = acl_presel->presel_id;
    block_id = acl_presel->lkup_level + (acl_presel->dir == CTC_EGRESS? ACL_IGS_BLOCK_MAX_NUM: 0);
    presel->pltb = &(SYS_ACL_LT_BLOCK(lchip, block_id));
    presel->flag = acl_presel->flag;
    presel->dir  = acl_presel->dir;
    if (FALSE == ctc_vector_add(SYS_ACL_PRESEL_VEC(lchip), acl_presel->presel_id, presel))
    {
        CTC_ERROR_GOTO(CTC_E_NO_RESOURCE, ret, error0);
    }

    if (acl_presel->flag & CTC_ACL_PRESEL_FLAG_ENTRY_PRIO)
    {
        presel->fpae.priority = acl_presel->entry_pri;
    }
    else
    {
        presel->fpae.priority = FPA_PRIORITY_DEFAULT;
    }
    
    if (!p_usw_acl_master[lchip]->glb_presel_en)
    {
         /* update max part_num value */
         _sys_tmm_acl_presel_expand_lt_block(lchip, block_id, fk->key_size);
            
        CTC_ERROR_GOTO(sys_tmm_acl_add_presel_tcam(lchip, presel), ret, error1);
        fk->ad_index = presel->fpae.offset_a;
        if (fk->key_size == 1)
        {
            CTC_BIT_SET(presel->pltb->is_key160, presel->fpae.offset_a);
        }
        CTC_BIT_SET(fk->presel_cnt, acl_presel->lkup_level);
    }
    else
    {
        for (loop=0; loop<SYS_ACL_PRESEL_NUM; loop++)
        {
            if (NULL == fk->presel_ent_arr[loop])
            {
                break;
            }
        }
        if (loop == SYS_ACL_PRESEL_NUM)
        {
            CTC_ERROR_GOTO(CTC_E_NO_RESOURCE, ret, error1);
        }

        presel->pltb = &(SYS_ACL_LT_BLOCK(lchip, block_base));
        CTC_ERROR_GOTO(sys_tmm_acl_add_presel_tcam(lchip, presel), ret, error1);
        if(fk->presel_cnt == 0)
        {
            _sys_tmm_acl_fk_alloc_ltid(lchip, block_base, fk, 0);
            fk->presel_ent_arr[loop] = presel;
            SYS_ACL_IGS_BLK_LOOP(loop, max_block_num)
            {
                fk->ltid[loop] = fk->ltid[0];
            }
            fk->ltid_valid = (1<<ACL_IGS_BLOCK_MAX_NUM)-1;
        }
        fk->presel_cnt++;
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);

    return CTC_E_NONE;

error1:
    ctc_vector_del(SYS_ACL_PRESEL_VEC(lchip), acl_presel->presel_id);
error0:
    mem_free(presel);
    mem_free(buffer);
    return ret;
}

uint32 _sys_tmm_acl_check_presel_cnt_cb(sys_acl_presel_t *presel, sys_acl_presel_t *presel_in)
{
    if(presel != presel_in && presel->pltb == presel_in->pltb && presel->fk == presel_in->fk)
    {
        return -1;
    }
    return 0;
}


int32
sys_tmm_acl_destroy_presel(uint8 lchip, uint16 presel_id)
{
    uint8  loop = 0;
    uint8  max_block_num = 0;
    uint8  block_id = 0;
    uint8  block_base = 0;
    sys_acl_presel_t *presel = NULL;
    ctc_field_key_t presel_valid;

    presel = ctc_vector_get(SYS_ACL_PRESEL_VEC(lchip), presel_id);
    if (NULL == presel || NULL == presel->fk)
    {
        CTC_ERROR_RETURN(CTC_E_NOT_EXIST);
    }

    if (SYS_ACL_FK_IS_HASH(presel->fk))
    {
        return sys_at_acl_destroy_hash_presel(lchip, presel);
    }
    
    max_block_num = (presel->dir==CTC_INGRESS)?(ACL_IGS_BLOCK_MAX_NUM):(ACL_EGS_BLOCK_MAX_NUM);
    SYS_ACL_IGS_BLK_LOOP(loop, max_block_num)
    {
        if (presel->fk->entry_count[loop])
        {
            CTC_ERROR_RETURN(CTC_E_IN_USE);
        }
    }

    block_id = presel->pltb->block_id;
    block_base = SYS_ACL_BLOCK_BASE(block_id);
    
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL, 1);

    _sys_tmm_acl_presel_compress_lt_block(lchip, block_id, presel->fk->key_size, presel_id);

    if (presel->flag & CTC_ACL_PRESEL_FLAG_LKUP_LEVEL)
    {
        if (presel->fpae.flag == FPA_ENTRY_FLAG_INSTALLED)
        {
            sal_memset(&presel_valid, 0, sizeof(ctc_field_key_t));
            presel_valid.type = CTC_FIELD_KEY_PRESEL_ENTRY_VALID;
            presel_valid.data = 0;
            sys_tmm_acl_add_presel_fields(lchip, presel_id, 1, &presel_valid);
        }
        else
        {
            /* The fk that the presel used, may ref by more than one presel in the same block */
            if(ctc_vector_traverse(SYS_ACL_PRESEL_VEC(lchip),  (vector_traversal_fn)_sys_tmm_acl_check_presel_cnt_cb, presel) == 0)
            {
                /* the presel is the last one use the fk in this block */
                CTC_BIT_UNSET(presel->fk->presel_cnt, block_id-block_base);
            }
        }
        if (presel->fk->key_size == 1)
        {
            CTC_BIT_UNSET(presel->pltb->is_key160, presel->fpae.offset_a);
        }
        sys_tmm_acl_remove_presel_tcam(lchip, presel);
    }
    else
    {
        presel->fk->presel_cnt--;
        sys_tmm_acl_remove_presel_tcam(lchip, presel);
        if(presel->fk->presel_cnt == 0)
        {
            _sys_tmm_acl_fk_free_ltid(lchip, block_id, presel->fk);
            SYS_ACL_IGS_BLK_LOOP(loop, max_block_num)
            {
                presel->fk->ltid[loop] = SYS_ACL_INVALID_LTID;
            }
            presel->fk->ltid_valid = 0;
        }
        for (loop=0; loop<SYS_ACL_PRESEL_NUM; loop++)
        {
            if (presel->fk->presel_ent_arr[loop] == presel)
            {
                presel->fk->presel_ent_arr[loop] = NULL;
            }
        }
    }

    ctc_vector_del(SYS_ACL_PRESEL_VEC(lchip), presel_id);
    if (NULL != presel->buffer)
    {
        mem_free(presel->buffer);
    }
    mem_free(presel);
    return CTC_E_NONE;
}
STATIC void
_sys_tmm_acl_show_presel(uint8 lchip, uint8 detail, uint16 cnt, sys_acl_presel_t *presel)
{
    uint32 key_index = 0;
    uint32 hw_table[][CTC_BOTH_DIRECTION] = {
       {DsLtidSelectTcam_t,  DsEgrLtidSelectTcam_t},
       {DsLtidSelectAd0_t, DsEgrLtidSelectAd0_t},
       {DsProgramKeyGenProfile0_t, DsEgrProgramKeyGenProfile0_t}
    };
    char   block_str[4] = "--";
    uint8  block_id = 0;
    uint8  is_installed = 0;

    if (presel->fk && SYS_ACL_FK_IS_HASH(presel->fk))
    {
        if (!detail)
        {
            is_installed = (presel->fpae.flag == FPA_ENTRY_FLAG_INSTALLED);
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-6d%-12d%-8u%-4s%-12s%-8s%-16s\n",
                        cnt,presel->presel_id,presel->lkup_level,is_installed? "Y": "N",
                        "--","--",sys_tmm_acl_get_key_type_name(presel->fk->key_type));
        }
        return;
    }
    if (presel->flag & CTC_ACL_PRESEL_FLAG_LKUP_LEVEL)
    {
        block_id = SYS_ACL_BLOCK_PRIO(presel->pltb->block_id);
        sal_sprintf(block_str, "%d", block_id);
    }
    SYS_ACL_ENCODE_PRESEL_TCAM_INDEX(block_id, presel->fpae.offset_a, key_index);
    if (detail)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "#PRESEL_ID: %-12d  Table:\n", presel->presel_id);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, " --%-27s  :%-d\n", DRV_TABLE_NAME(lchip, hw_table[0][presel->dir]), key_index);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, " --%-27s  :%-d\n", DRV_TABLE_NAME(lchip, hw_table[1][presel->dir]+block_id), presel->fpae.offset_a);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, " --%-27s  :%-d\n\n", DRV_TABLE_NAME(lchip, hw_table[2][presel->dir]+block_id), presel->fpae.offset_a);
    }
    else
    {
        is_installed = (presel->fpae.flag == FPA_ENTRY_FLAG_INSTALLED);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-6d%-12d%-8s%-4s%-12d%-8d%-16s\n",
                        cnt,presel->presel_id,block_str,is_installed? "Y": "N",
                        presel->fpae.priority,key_index,sys_tmm_acl_get_key_type_name(presel->fk->key_type));
    }
}
STATIC int32
_sys_tmm_acl_presel_traversal_fn(void* node, void* user_data)
{
    uint8* p_data = user_data;
    uint8  lchip = p_data[0];
    uint8  detail = p_data[1];
    uint16 cnt = (p_data[2]<<8)+p_data[3];
    sys_acl_presel_t* presel = node;
    if (presel)
    {
        _sys_tmm_acl_show_presel(lchip, detail, cnt++, presel);
        p_data[2] = cnt>>8;
        p_data[3] = cnt&0xFF;
    }
    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_replace_update_presel_fn(void* node, void* user_data)
{
    sys_acl_lt_block_t ** pp_ltb = (sys_acl_lt_block_t **)user_data;
    sys_acl_presel_t* presel = node;

    if (presel && presel->pltb == pp_ltb[0])
    {
        presel->pltb = pp_ltb[1];
    }
    return CTC_E_NONE;
}

int32
sys_tmm_acl_show_presel(uint8 lchip, uint8 type, uint16 presel_id, uint8 detail)
{
    uint8  user_data[4] = {0};
    sys_acl_presel_t *presel = NULL;

    SYS_ACL_INIT_CHECK();
    CTC_MAX_VALUE_CHECK(presel_id, SYS_ACL_PRESEL_ID_MAX);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-6s%-12s%-8s%-4s%-12s%-8s%-16s\n",
                    "No.","PRESEL_ID","BLOCK","HW","E_PRI","INDEX","KEY_TYPE");
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "------------------------------------------------------------------\n");
    user_data[0] = lchip;
    if (!type)  /*all*/
    {
        user_data[1] = 0;
        ctc_vector_traverse(SYS_ACL_PRESEL_VEC(lchip), _sys_tmm_acl_presel_traversal_fn, user_data);
    }
    else    /*by presel id*/
    {
        presel = ctc_vector_get(SYS_ACL_PRESEL_VEC(lchip), presel_id);
        if (NULL == presel)
        {
            CTC_ERROR_RETURN(CTC_E_ENTRY_NOT_EXIST);
        }
        _sys_tmm_acl_show_presel(lchip, 0, 0, presel);
    }
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "\n");
    /*detail*/
    if (detail)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "*If presel entry occupy more than one block, only show first block.\n");
        if (!type)  /*all*/
        {
            user_data[1] = 1;
            ctc_vector_traverse(SYS_ACL_PRESEL_VEC(lchip), _sys_tmm_acl_presel_traversal_fn, user_data);
        }
        else    /*by presel id*/
        {
            _sys_tmm_acl_show_presel(lchip, 1, 0, presel);
        }
    }

    return CTC_E_NONE;
}

#define SYS_ACL_GET_PRESEL_ENTRY_VALID(fields_cnt, fields, valid)    \
    do{\
        uint32 _loop_ = 0;\
        for (_loop_=0; _loop_<fields_cnt; _loop_++){\
            if (fields[_loop_].type == CTC_FIELD_KEY_PRESEL_ENTRY_VALID) {\
                CTC_MAX_VALUE_CHECK(fields[_loop_].data, 1);\
                valid = fields[_loop_].data;\
            }\
        }\
    }while(0)
int32
sys_tmm_acl_add_presel_fields(uint8 lchip, uint16 presel_id, uint8 fields_cnt, ctc_field_key_t *fields)
{
    uint16 loop = 0;
    uint8  block_id = 0;
    uint8  lkup_level = 0;
    uint8  entry_valid = 0xFF;
    uint8  fk_part_num = 0;
    uint8  part = 0;
    uint8  loop1 = 0;
    uint8  max_block_num = ACL_IGS_BLOCK_MAX_NUM;
    uint8  block_base = 0;
    uint8 global_pp_en = 0;
    sys_acl_presel_t    *presel = NULL;
    sys_acl_lt_block_t  *p_ltb = NULL;
    sys_acl_block_t     *p_acb = NULL;
    sys_acl_league_t    *p_league = NULL;
    sys_acl_flex_key_t  *fk = NULL;
    sys_acl_presel_temp_entry_t temp_entry;
    tbl_entry_t buffer;
    uint32 *temp_key = NULL;
    uint32 *temp_mask = NULL;
    DsProgramKeyGenProfile_m temp_profile;
    DsLtidSelectAd_m temp_ad;

    presel = ctc_vector_get(SYS_ACL_PRESEL_VEC(lchip), presel_id);
    if (NULL == presel)
    {
        CTC_ERROR_RETURN(CTC_E_ENTRY_NOT_EXIST);
    }
    if (presel->fk && SYS_ACL_FK_IS_HASH(presel->fk))
    {
        return sys_at_acl_add_presel_fields(lchip, presel, fields_cnt, fields, TRUE);
    }
    if (presel->dir == CTC_EGRESS)
    {
        max_block_num = ACL_EGS_BLOCK_MAX_NUM;
        block_base = ACL_IGS_BLOCK_MAX_NUM;
    }
    SYS_ACL_GET_PRESEL_ENTRY_VALID(fields_cnt, fields, entry_valid);
    if ((presel->fpae.flag == FPA_ENTRY_FLAG_INSTALLED) && (entry_valid != 0))
    {
        CTC_ERROR_RETURN(CTC_E_ENTRY_EXIST);
    }
    if (presel->fpae.flag == FPA_ENTRY_FLAG_UNINSTALLED)
    {
        /*sal_memcpy(&temp_key, presel->buffer->key, sizeof(DsLtidSelectTcam_m));
        sal_memcpy(&temp_mask, presel->buffer->mask, sizeof(DsLtidSelectTcam_m));*/
        temp_key = presel->buffer->key;
        temp_mask = presel->buffer->mask;
        buffer.data_entry = (uint32*)temp_key;
        buffer.mask_entry = (uint32*)temp_mask;
        for (loop=0; loop<fields_cnt; loop++)
        {
            CTC_ERROR_RETURN(sys_tmm_acl_build_presel_field(lchip, presel->dir, buffer, &fields[loop], TRUE));
        }
    }
    fk = presel->fk;
    fk_part_num = fk->key_size;
    if (presel->flag & CTC_ACL_PRESEL_FLAG_LKUP_LEVEL)
    {
        SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);
        SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL, 1);
        block_id = presel->pltb->block_id;
        lkup_level = SYS_ACL_BLOCK_PRIO(block_id);
        global_pp_en = SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, presel->dir, (lkup_level));
        p_league = &p_usw_acl_master[global_pp_en ? SYS_PP_BASE(lchip) : lchip]->league[block_id];
        p_acb = &p_usw_acl_master[global_pp_en ? SYS_PP_BASE(lchip) : lchip]->block[block_id];
        p_ltb = presel->pltb;
        sal_memset(&temp_ad, 0, sizeof(DsLtidSelectAd_m));

        if ((presel->fpae.flag == FPA_ENTRY_FLAG_UNINSTALLED) && (entry_valid == 1))    /*install*/
        {
            SYS_ACL_PART_LOOP(part, fk_part_num)
            {
                CTC_ERROR_RETURN(_sys_tmm_acl_fk_alloc_ltid(lchip, block_id+part, fk, part));
            }
            temp_entry.key.data_entry = (uint32*)temp_key;
            temp_entry.key.mask_entry = (uint32*)temp_mask;
            temp_entry.profile = &temp_profile;
            SYS_ACL_PART_LOOP(part, fk_part_num)
            {
                temp_entry.ad = (void*)&temp_ad;
                _sys_tmm_acl_fk_build_kgen_profile(lchip, part, fk, &temp_profile);
                _sys_tmm_acl_fk_build_presel_ad(lchip, part, fk->ltid[lkup_level+part], fk, temp_entry.ad);
                sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_ADD, presel->fpae.offset_a, &temp_entry, block_base, 0);
            }
            if (fk_part_num == 1 && p_acb->fpab.part_num > 1)
            {
                for (part=1;part<p_acb->fpab.part_num; part++)
                {
                    sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_COPY, presel->fpae.offset_a, NULL, block_id, 0);
                }
            }
            if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST)
            {
                SYS_ACL_IGS_BLK_LOOP(loop1, max_block_num)
                {
                    SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, loop1));
                    SYS_ACL_IF_CONTINUE(p_league->size < fk_part_num);
                    SYS_ACL_IF_CONTINUE(loop1 == block_id-block_base);
                    fk_part_num = (fk_part_num == 1 && p_acb->fpab.part_num > 1) ? p_acb->fpab.part_num : fk_part_num;
                    SYS_ACL_PART_LOOP(part, fk_part_num)
                    {
                        sys_tmm_acl_op_presel_hw(lchip, loop1+block_base+part, SYS_ACL_PRESEL_OP_MODE_COPY, presel->fpae.offset_a, NULL, block_id+part, 0);
                    }
                }
            }
            if (fk_part_num == 1)
            {
                CTC_BIT_SET(p_ltb->is_key160, presel->fpae.offset_a);
            }
            presel->fpae.flag = FPA_ENTRY_FLAG_INSTALLED;
            mem_free(presel->buffer);
        }
        else if ((presel->fpae.flag == FPA_ENTRY_FLAG_INSTALLED) && (entry_valid == 0)) /*uninstall*/
        {
            max_block_num = (presel->dir==CTC_INGRESS)?(ACL_IGS_BLOCK_MAX_NUM):(ACL_EGS_BLOCK_MAX_NUM);
            SYS_ACL_IGS_BLK_LOOP(loop, max_block_num)
            {
                if (presel->fk->entry_count[loop])
                {
                    CTC_ERROR_RETURN(CTC_E_IN_USE);
                }
            }

            sys_tmm_acl_resume_presel_tcam_hw(lchip, lkup_level, presel);

            /* The fk that the presel used, may ref by more than one presel in the same block */
            if(ctc_vector_traverse(SYS_ACL_PRESEL_VEC(lchip),  (vector_traversal_fn)_sys_tmm_acl_check_presel_cnt_cb, presel) == 0)
            {
                /* the presel is the last one use the fk in this block */
                CTC_BIT_UNSET(presel->fk->presel_cnt, block_id-block_base);
            }

            if (fk_part_num == 1)
            {
                CTC_BIT_UNSET(p_ltb->is_key160, presel->fpae.offset_a);
            }
            if (p_acb->block_status == SYS_ACL_BLOCK_STATUS_MERGE_DST)
            {
                SYS_ACL_IGS_BLK_LOOP(loop1, max_block_num)
                {
                    SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_league->lkup_level_bitmap, loop1));
                    SYS_ACL_IF_CONTINUE(p_league->size < fk_part_num);
                    SYS_ACL_IF_CONTINUE(loop1 == lkup_level );
                    fk_part_num = (fk_part_num == 1 && p_acb->fpab.part_num > 1) ? p_acb->fpab.part_num : fk_part_num;
                    SYS_ACL_PART_LOOP(part, fk_part_num)
                    {
                        sys_tmm_acl_op_presel_hw(lchip, loop1+block_base+part, SYS_ACL_PRESEL_OP_MODE_DEL, presel->fpae.offset_a, NULL, block_base, 0);
                    }
                }
            }
            if (fk_part_num == 1 && p_acb->fpab.part_num > 1)
            {
                for (part=1;part<p_acb->fpab.part_num; part++)
                {
                    sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_DEL, presel->fpae.offset_a, NULL, block_base, 0);
                }
            }
            SYS_ACL_PART_LOOP(part, fk_part_num)
            {
                sys_tmm_acl_op_presel_hw(lchip, block_id+part, SYS_ACL_PRESEL_OP_MODE_DEL, presel->fpae.offset_a, NULL, 0, 0);
                if(CTC_IS_BIT_SET(fk->presel_cnt, block_id) )
                {
                    continue;
                }
                _sys_tmm_acl_fk_free_ltid(lchip, block_id+part, fk);
            }
            presel->fpae.flag = FPA_ENTRY_FLAG_UNINSTALLED;
        }
    }

    return CTC_E_NONE;
}
int32
sys_tmm_acl_remove_presel_fields(uint8 lchip, uint16 presel_id, uint8 fields_cnt, ctc_field_key_t *fields)
{
    uint16 loop = 0;
    sys_acl_presel_t *presel = NULL;
    tbl_entry_t buffer;
    DsLtidSelectTcam_m temp_key, temp_mask;

    presel = ctc_vector_get(SYS_ACL_PRESEL_VEC(lchip), presel_id);
    if (NULL == presel)
    {
        CTC_ERROR_RETURN(CTC_E_ENTRY_NOT_EXIST);
    }
    if (presel->fpae.flag == FPA_ENTRY_FLAG_INSTALLED)
    {
        CTC_ERROR_RETURN(CTC_E_NOT_READY);
    }
    if (presel->fk && SYS_ACL_FK_IS_HASH(presel->fk))
    {
        return sys_at_acl_add_presel_fields(lchip, presel, fields_cnt, fields, FALSE);
    }
    sal_memcpy(&temp_key, presel->buffer->key, sizeof(DsLtidSelectTcam_m));
    sal_memcpy(&temp_mask, presel->buffer->mask, sizeof(DsLtidSelectTcam_m));
    buffer.data_entry = (uint32*)&temp_key;
    buffer.mask_entry = (uint32*)&temp_mask;

    for (loop=0; loop<fields_cnt; loop++)
    {
        CTC_ERROR_RETURN(sys_tmm_acl_build_presel_field(lchip, presel->dir, buffer, &fields[loop], FALSE));
    }
    sal_memcpy(presel->buffer->key, &temp_key, sizeof(DsLtidSelectTcam_m));
    sal_memcpy(presel->buffer->mask, &temp_mask, sizeof(DsLtidSelectTcam_m));
    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_default_key_init(uint8 lchip,
                              uint8 ltid,
                              uint8 dir,
                              uint8 key_type,
                              sys_acl_kset_t* kset)
{
    uint8  loop = 0;
    uint8  max_block_num = (dir==CTC_INGRESS)?(ACL_IGS_BLOCK_MAX_NUM):(ACL_EGS_BLOCK_MAX_NUM);
    int32  ret = CTC_E_NONE;
    ctc_acl_flex_key_t acl_fk;
    sys_acl_flex_key_t *dft_fk = NULL;
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    if (!DRV_FROM_AT(lchip) && dir == CTC_EGRESS)
    {
        return CTC_E_NONE;
    }

    MALLOC_ZERO(MEM_ACL_MODULE, dft_fk, SYS_ACL_FK_SIZE(max_block_num, sys_acl_flex_key_t))
    if (NULL == dft_fk)
    {
        CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
    }
    dft_fk->entry_count = (void*)dft_fk + sizeof(sys_acl_flex_key_t);
    if (FALSE == ctc_vector_add(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(dir, key_type), dft_fk))
    {
        CTC_ERROR_GOTO(CTC_E_NO_RESOURCE, ret, error0)
    }
    dft_fk->key_type = key_type;
    dft_fk->node_type = (dir == CTC_INGRESS)?(SYS_ACL_FLEX_KEY_NODE_I_TCAM):(SYS_ACL_FLEX_KEY_NODE_E_TCAM);
    SYS_ACL_IGS_BLK_LOOP(loop, max_block_num)
    {
        dft_fk->ltid[loop] = ltid;
        CTC_BIT_SET(dft_fk->ltid_valid, loop);
    }
    sal_memcpy(&dft_fk->kset, kset, sizeof(sys_acl_kset_t));
    sal_memset(&acl_fk, 0, sizeof(ctc_acl_flex_key_t));
    acl_fk.ipv6_sa_bmp = acl_fk.ipv6_da_bmp = 0xF;
    //nsh -- acl_fk.nsh_ch_bmp = 0xF;
    acl_fk.sudf_bmp = acl_fk.udf_bmp = 0xFF;
    acl_fk.compress_ether_type = FALSE;
    acl_fk.dir = dir;
    _sys_tmm_acl_flex_key_update_kset(lchip, dft_fk, &acl_fk);
    CTC_ERROR_GOTO(_sys_tmm_acl_build_extract_db(lchip, SYS_ACL_FK_SIZE_MODE_AUTO, dft_fk), ret, error1);  /*no alloc*/
    return CTC_E_NONE;
error1:
    ctc_vector_del(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), SYS_ACL_FLEX_KEY_IDX(dir, key_type));
error0:
    SYS_ACL_PART_LOOP(loop, dft_fk->key_size)
    {
        mem_free(dft_fk->field_arr[loop].offset_arr);
        mem_free(dft_fk->field_arr[loop].qid_arr);
    }
    mem_free(dft_fk);
    return ret;
}

STATIC int32
_sys_tmm_acl_flex_key_init(uint8 lchip)
{
    uint8  key_type = 0;
    uint8  ltid = 0;
    sys_acl_kset_t kset;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    CTC_ERROR_RETURN(_sys_tmm_acl_dft_ltid_init(lchip));


    //_sys_usw_acl_add_mackey160_field
    key_type = CTC_ACL_KEY_MAC;  /*320bits  Requst BitCnt:185*/
    ltid = SYS_ACL_KEY_LTID_MAC;
    sal_memset(&kset, 0, sizeof(sys_acl_kset_t));

    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);

    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    sal_memset(&kset, 0, sizeof(sys_acl_kset_t));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TARGET_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FID    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MIRROR_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_GPORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_HDR_EXIST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PRIORITY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_XLATE_HIT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_EGRESS, key_type, &kset));


    //_sys_usw_acl_add_l3key160_field
    key_type = CTC_ACL_KEY_IPV4;   /*320bits Requst BitCnt:243*/
    ltid = SYS_ACL_KEY_LTID_IPV4;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER);
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MIRROR_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_HDR_EXIST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PRIORITY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_EGRESS, key_type, &kset));

    //_sys_usw_acl_add_l3key320_field
    key_type = CTC_ACL_KEY_IPV4_EXT; /*320 Requst BitCnt:319*/
    ltid = SYS_ACL_KEY_LTID_IPV4_EXT;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_CBIT);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_NEXT_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_OBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SPI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);  /*For RFC2474,SDK support CTC_FIELD_KEY_IP_DSCP, CTC_FIELD_KEY_IP_ECN;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE); /*For RFC1349,SDK support CTC_FIELD_KEY_IP_PRECEDENCE;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1 );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2 );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER);
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_CTL_PKT);
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_AWARE_TUNNEL_INFO);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_GPORT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MIRROR_PKT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PRIORITY );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_HDR_EXIST );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FID        );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT      );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_CBIT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_NEXT_PROTOCOL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_OBIT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SI );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SPI );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT      );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA  );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID        );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID        );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL  );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG  );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS         );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID      );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1  );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2  );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL        );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE        );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION        );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID  );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE  );
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI  );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER );
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE      );
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_MAC            );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_ID          );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_CTL_PKT          );
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID      );
	if(DRV_FROM_AT(lchip))
	{
	    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_AWARE_TUNNEL_INFO);
	}
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID );
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_EGRESS, key_type, &kset));

    //_sys_usw_acl_add_macl3key320_field
    key_type = CTC_ACL_KEY_MAC_IPV4;  /*480 Requst BitCnt:399*/
    ltid = SYS_ACL_KEY_LTID_MAC_IPV4;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);  /*For RFC2474,SDK support CTC_FIELD_KEY_IP_DSCP, CTC_FIELD_KEY_IP_ECN;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE); /*For RFC1349,SDK support CTC_FIELD_KEY_IP_PRECEDENCE;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TARGET_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER);
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_AWARE_TUNNEL_INFO);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_HDR_EXIST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_GPORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MIRROR_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);/*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TARGET_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_EGRESS, key_type, &kset));

    //_sys_usw_acl_add_macl3key640_field
    key_type = CTC_ACL_KEY_MAC_IPV4_EXT;  /*480 Requst BitCnt:453*/
    ltid = SYS_ACL_KEY_LTID_MAC_IPV4_EXT;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_CBIT);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_NEXT_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_OBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SPI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);  /*For RFC2474,SDK support CTC_FIELD_KEY_IP_DSCP, CTC_FIELD_KEY_IP_ECN;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE); /*For RFC1349,SDK support CTC_FIELD_KEY_IP_PRECEDENCE;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1 );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2 );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TARGET_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER);
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_CTL_PKT);
    */
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_AWARE_TUNNEL_INFO);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_UDF);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_FLAGS    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_GPORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_HDR_EXIST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MIRROR_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PRIORITY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FID    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID    );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE    );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_CBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_NEXT_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_OBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SPI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1 );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2 );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TARGET_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE    );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_EGRESS, key_type, &kset));

    //_sys_usw_acl_add_ipv6key320_field
    key_type = CTC_ACL_KEY_IPV6;  /*480 Requst BitCnt:421*/
    ltid = SYS_ACL_KEY_LTID_IPV6;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);  /*For RFC2474,SDK support CTC_FIELD_KEY_IP_DSCP, CTC_FIELD_KEY_IP_ECN;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE); /*For RFC1349,SDK support CTC_FIELD_KEY_IP_PRECEDENCE;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_FLOW_LABEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID);  /*Ipv6Key320 only support 13bti*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_FLOW_LABEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FID);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_EGRESS, key_type, &kset));

    //_sys_usw_acl_add_ipv6key640_field
    key_type = CTC_ACL_KEY_IPV6_EXT;  /*480 Requst BitCnt:454*/
    ltid = SYS_ACL_KEY_LTID_IPV6_EXT;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);  /*For RFC2474,SDK support CTC_FIELD_KEY_IP_DSCP, CTC_FIELD_KEY_IP_ECN;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE); /*For RFC1349,SDK support CTC_FIELD_KEY_IP_PRECEDENCE;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_CBIT);*/
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_NEXT_PROTOCOL);
    ///SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_OBIT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SPI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_FLOW_LABEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_CTL_PKT);
    */
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_AWARE_TUNNEL_INFO);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_UDF);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_GPORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MIRROR_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_HDR_EXIST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PRIORITY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_CBIT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_NEXT_PROTOCOL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_OBIT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SPI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_FLOW_LABEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_EGRESS, key_type, &kset));

    //_sys_usw_acl_add_macipv6key640_field
    key_type = CTC_ACL_KEY_MAC_IPV6;  /*480 Requst BitCnt:475*/
    ltid = SYS_ACL_KEY_LTID_MAC_IPV6;
    sal_memset(&kset, 0, sizeof(kset));
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);  /*For RFC2474,SDK support CTC_FIELD_KEY_IP_DSCP, CTC_FIELD_KEY_IP_ECN;*/
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE); /*For RFC1349,SDK support CTC_FIELD_KEY_IP_PRECEDENCE;*/
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_FLOW_LABEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_RADIO_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_WLAN_CTL_PKT);
    */
  //  SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID);
  //  SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_AWARE_TUNNEL_INFO);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_GPORT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MIRROR_PKT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_HDR_EXIST );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PRIORITY );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FID    );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT         );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID );
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP  );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID                  );
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG  );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA         );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID             );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID             );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA           );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA           );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_FLOW_LABEL    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY     );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY    );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1    );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS          );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID      );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA      );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA     );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE    );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT );
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID );
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL      );
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VRFID );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_EGRESS, key_type, &kset));

    //_sys_usw_acl_add_cidkey160_field
    key_type = CTC_ACL_KEY_CID; /*480 Requst BitCnt:357*/
    ltid = SYS_ACL_KEY_LTID_CID;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);  /*For RFC2474,SDK support CTC_FIELD_KEY_IP_DSCP, CTC_FIELD_KEY_IP_ECN;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE); /*For RFC1349,SDK support CTC_FIELD_KEY_IP_PRECEDENCE;*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    //_sys_usw_acl_add_shortkey80_field
    key_type = CTC_ACL_KEY_INTERFACE; /*320 Requst BitCnt:102*/
    ltid = SYS_ACL_KEY_LTID_INTERFACE;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD );
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    //_sys_usw_acl_add_forwardkey320_field
    key_type = CTC_ACL_KEY_FWD; /*480 Requst BitCnt:460*/
    ltid = SYS_ACL_KEY_LTID_FWD;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_GPORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_NHID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACSA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACSA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACDA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACDA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPSA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPSA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPDA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPDA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PKT_FWD_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    /*
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER);
    */
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_LOG_PKT);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    //_sys_usw_acl_add_forwardkey640_field
    key_type = CTC_ACL_KEY_FWD_EXT; /*Requst BitCnt:468*/
    ltid = SYS_ACL_KEY_LTID_FWD_EXT;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_GPORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_NHID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SRC_CID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DECAP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ELEPHANT_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MCAST_RPF_CHECK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACSA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACSA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACDA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACDA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPSA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPSA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPDA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPDA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PKT_FWD_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_FLOW_LABEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GRE_KEY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NVGRE_KEY);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV1);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_RSV2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VXLAN_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TARGET_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_AWARE_TUNNEL_INFO);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_UDF);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_CBIT);*/
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_NEXT_PROTOCOL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_OBIT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_NSH_SPI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_LOG_PKT);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    //_sys_usw_acl_add_coppkey320_field
    key_type = CTC_ACL_KEY_COPP; /*480 Requst BitCnt:376*/
    ltid = SYS_ACL_KEY_LTID_COPP;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_NHID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACSA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACSA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACDA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACDA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPSA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPSA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPDA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPDA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PKT_FWD_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_STATION_MOVE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SECURITY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STP_STATE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_LOG_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    //_sys_usw_acl_add_coppkey640_field
    key_type = CTC_ACL_KEY_COPP_EXT; /*480 Requst BitCnt:473*/
    ltid = SYS_ACL_KEY_LTID_COPP_EXT;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_RARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DST_NHID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_USER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CPU_REASON_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INTERFACE_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACSA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACSA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACDA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MACDA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPSA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPSA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPDA_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPDA_HIT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PKT_FWD_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_STATION_MOVE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MAC_SECURITY_DISCARD);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ROUTED_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DSCP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PRECEDENCE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_SA);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IPV6_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_SA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_DA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ICMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IGMP_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PKT_LEN_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT_RANGE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_ECN);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TCP_FLAGS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_PROTOCOL_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDER_IP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGET_IP);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SENDER_MAC);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TARGET_MAC);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_SA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_MAC_DA_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_SENDERIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ARP_TARGETIP_CHK_FAIL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GARP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_LEVEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_OP_CODE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_OAM_VERSION);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_Y1731_OAM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_LABEL_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL0);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL1);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_LABEL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_EXP2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_SBIT2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_MPLS_TTL2);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_DST_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_FCOE_SRC_FCID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_TTL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_INGRESS_NICKNAME);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_EGRESS_NICKNAME);
    /*SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_ESADI);*/
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_TRILL_CHANNEL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_INNER_VLANID_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_LENGTH);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTIHOP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_MULTICAST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_TRILL_VERSION);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_CODE);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_FLAGS);
   // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SLOW_PROTOCOL_SUB_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_MESSAGE_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PTP_VERSION);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_MEF_OUI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_OUI_SUB_TYPE);
    // SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SATPDU_PDU_BYTE);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_AN);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_ES);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_PN);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SC);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCI);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SL);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_UNKNOW_PKT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_CBIT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_EBIT);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_SCB);
    //SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_DOT1AE_VER);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STP_STATE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IS_LOG_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_SVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CVLAN_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_VALID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_CFI);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CTAG_COS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STAG_COS);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    //_sys_usw_acl_add_udfkey320_field
    key_type = CTC_ACL_KEY_UDF; /*480 Requst BitCnt:473*/
    ltid = SYS_ACL_KEY_LTID_UDF;
    sal_memset(&kset, 0, sizeof(kset));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_CLASS_ID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_ETHER_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_HDR_ERROR);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_OPTIONS);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_FRAG);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_DST_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L2_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_VLAN_NUM);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_IP_PROTOCOL);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L3_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_L4_TYPE);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_GEM_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_METADATA);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_UDF);
    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    //_sys_usw_acl_add_cfkey160_field
    key_type = CTC_ACL_KEY_STK_HDR;  /*160bits  Requst BitCnt:92*/
    ltid = SYS_ACL_KEY_LTID_STK_HDR;
    sal_memset(&kset, 0, sizeof(sys_acl_kset_t));
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_HDR_EXIST);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_LKUP);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_DST_GPORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_FID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_LOGIC_SRC_PORT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_MIRROR_PKT);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_PRIORITY);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_SRC_CID);
    SYS_ACL_KSET_ADD_FIELD(CTC_FIELD_KEY_STK_SRC_GPORT);

    CTC_ERROR_RETURN(_sys_tmm_acl_default_key_init(lchip, ltid, CTC_INGRESS, key_type, &kset));

    return 0;
}

STATIC int32
_sys_tmm_acl_show_extract(uint8 lchip, sys_acl_ext_sel_t *p_sel)
{
    uint8 i = 0;

    for (i = 0; i < SYS_ACL_EXT_L1_E32_MAX; i++)
    {
        if (p_sel->l1_e32_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l1_e32_sel[%d] = %d\n", i, p_sel->l1_e32_sel[i]);
    }

    for (i = 0; i < SYS_ACL_EXT_L1_E16_MAX; i++)
    {
        if (p_sel->l1_e16_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l1_e16_sel[%d] = %d\n", i, p_sel->l1_e16_sel[i]);
    }

    for (i = 0; i < SYS_ACL_EXT_L1_E8_MAX; i++)
    {
        if (p_sel->l1_e8_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l1_e8_sel[%d] = %d\n", i, p_sel->l1_e8_sel[i]);
    }

    for (i = 0; i < SYS_ACL_EXT_L1_E4_MAX; i++)
    {
        if (p_sel->l1_e4_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l1_e4_sel[%d] = %d\n", i, p_sel->l1_e4_sel[i]);
    }

    for (i = 0; i < SYS_ACL_EXT_L1_E2_MAX; i++)
    {

        if (p_sel->l1_e2_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l1_e2_sel[%d] = %d\n", i, p_sel->l1_e2_sel[i]);
    }

    for (i = 0; i < SYS_ACL_EXT_L2_E16_MAX; i++)
    {
        if (p_sel->l2_e16_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l2_e16_sel[%d] = %d\n", i, p_sel->l2_e16_sel[i]);
    }


    for (i = 0; i < SYS_ACL_EXT_L3_E1_MAX; i++)
    {
        if (p_sel->l3_e1_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l3_e1_sel[%d] = %d\n", i, p_sel->l3_e1_sel[i]);
    }

    for (i = 0; i < SYS_ACL_EXT_L3_E2_MAX; i++)
    {
        if (p_sel->l3_e2_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l3_e2_sel[%d] = %d\n", i, p_sel->l3_e2_sel[i]);
    }

    for (i = 0; i < SYS_ACL_EXT_L3_E4_MAX; i++)
    {
        if (p_sel->l3_e4_sel[i] == -1)
        {
            continue;
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "l3_e4_sel[%d] = %d\n", i, p_sel->l3_e4_sel[i]);
    }

    for (i = SYS_ACL_KEXT_CTRL_SEL_START; i < SYS_ACL_KEXT_CTRL_SEL_MAX; i++)
    {
        if (p_sel->sel_ctl[i] != -1)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "sel_ctl[%d] = %d\n", i,  p_sel->sel_ctl[i]);
        }
    }

   return 0;


}

STATIC int32
_sys_tmm_acl_show_offset(uint8 lchip, sys_acl_group_field_offset_info_t *p_offset)
{
    uint8 loop1 = 0;
    uint8 loop2 = 0;
    uint16 field_id = 0;
    uint16 sum = 0;

    for (loop1 = 0; loop1 < p_offset->size; loop1++)
    {
        field_id = p_offset->qid_arr[loop1];

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "\nField_ID:%d %s\n", field_id, ifp_field_str[field_id]);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "-------------------------\n");
        for (loop2 = 0; loop2 < p_offset->offset_arr[loop1].num_offsets; loop2++)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%d offset:%d, width:%d\n", loop2,
                            p_offset->offset_arr[loop1].offset[loop2],
                            p_offset->offset_arr[loop1].width[loop2]);

            sum +=  p_offset->offset_arr[loop1].width[loop2];
        }

    }

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "============================================\n");
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Total bits:%d\n", sum);

    return 0;
}

int32
sys_tmm_acl_show_flex_key_extract(uint8 lchip, sys_acl_flex_key_t *fk)
{
    uint8 loop = 0;
    uint8 part_num = 0;

    part_num = fk->key_size;

    for (loop = 0; loop < part_num; loop++)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "part(%d)\n", loop);
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "==========================================\n");
        _sys_tmm_acl_show_extract(lchip, &fk->ext_codes[loop]);
        _sys_tmm_acl_show_offset(lchip, &fk->field_arr[loop]);
    }

    return 0;
}

#if 0
int32
sys_tmm_acl_show_fixed_kgen_extract(uint8 lchip, uint8 key_type, uint8 pri_en, uint8 priority)
{
    sys_acl_flex_group_t* fg = NULL;

    if (pri_en)
    {
        if (p_usw_acl_master[lchip]->p_fixed_fg_user == NULL)
        {
            return CTC_E_INVALID_CONFIG;
        }

        fg = p_usw_acl_master[lchip]->p_fixed_fg_user + priority*CTC_ACL_KEY_NUM + key_type;
    }
    else
    {

        fg = p_usw_acl_master[lchip]->p_fixed_fg +  key_type;
    }

    if (fg == NULL)
    {
        return CTC_E_INVALID_CONFIG;
    }

    sys_tmm_acl_show_kgen_extract(lchip, fg);

    return 0;
}
#endif

int32
sys_tmm_acl_show_flex_keygen(uint8 lchip, uint8 dir, uint8 key_type, uint32 eid, uint32* data)
{
    sys_acl_flex_key_t* fk = NULL;
    sys_acl_entry_t* pe = NULL;
    uint32 key_id = 0, act_id = 0, hw_index = 0;
    uint8  part = 0, part_num = 0;
    ds_t buffer_key_tmp = {0};

    if (data != NULL)
    {
        _sys_usw_acl_get_entry_by_eid(lchip, eid, &pe);
        if (!(pe && ACL_ENTRY_IS_TCAM(pe) && SYS_ACL_GROUP_IS_PRESEL(pe->group)) )
        {
            return 0;
        }
        SYS_ACL_GET_TBL_FUNC(lchip, pe, &key_id, &act_id, &hw_index);
        if (FPA_ENTRY_FLAG_INSTALLED == pe->fpae.flag)
        {
            CTC_ERROR_RETURN(_sys_usw_acl_rebuild_buffer_from_hw(lchip, key_id, act_id, hw_index, pe));
        }
        sal_memcpy(buffer_key_tmp, pe->ua->buffer.key, sizeof(ds_t));
        sal_memcpy(pe->ua->buffer.key, data, 5*sizeof(uint32));
        SYS_ACL_KEY_SIZE_TO_PART_NUM(pe->ua->buffer.fk->key_size, part_num);
        for (part = 0; part < part_num; part++)
        {
            CTC_BMP_COPY_RANGE(pe->ua->buffer.key,  part*160, data, part*160, 160);
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Program Key Decoder\n==========================================\n");
        sys_tmm_acl_show_key_field(lchip, pe, TRUE);
        if (FPA_ENTRY_FLAG_INSTALLED == pe->fpae.flag)
        {
            _sys_usw_acl_map_action_data(lchip, pe, TRUE);
            CTC_ERROR_RETURN(sys_usw_acl_buffer_malloc(lchip, pe, FALSE));
        }
        else if(FPA_ENTRY_FLAG_UNINSTALLED == pe->fpae.flag)
        {
            sal_memcpy(pe->ua->buffer.key, buffer_key_tmp, sizeof(ds_t));
        }
        return 0;
    }
    else
    {
        fk = sys_tmm_acl_get_flex_key(lchip, dir, key_type);
        if (NULL == fk)
        {
            CTC_ERROR_RETURN(CTC_E_NOT_EXIST);
        }
        if (SYS_ACL_FK_IS_HASH(fk))
        {
            return 0;
        }
        sys_tmm_acl_show_flex_key_extract(lchip, fk);
    }

    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_reg_init(uint8 lchip)
{
    uint8  loop = 0;
    uint32 cmd = 0;
    ProgramAclLtidSelectConfig_m config;
    EpeAclTcamSlicePriority_m epe_acl_prio;
    EpeAclLkpSelectTcamSlice_m lkup_select;
    ProgramTcamAclCtl_m program_acl_ctl;
    IpeAclQosCtl_m acl_qos_ctl;
    EgrProgramTcamAclCtl1_m egr_prog_acl_ctl1;
    sys_acl_league_glb_config_t glb_config;

    sal_memset(&config, 0, sizeof(ProgramAclLtidSelectConfig_m));
    SetProgramAclLtidSelectConfig(V, categoryId0Sel_f, &config, 7);
    SetProgramAclLtidSelectConfig(V, categoryId1Sel_f, &config, 7);
    SetProgramAclLtidSelectConfig(V, categoryId2Sel_f, &config, 7);
    SetProgramAclLtidSelectConfig(V, categoryId3Sel_f, &config, 7);
    cmd = DRV_IOW(ProgramAclLtidSelectConfig_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &config));
    cmd = DRV_IOW(EgrProgramAclLtidSelectConfig_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &config));

    sal_memset(&program_acl_ctl, 0, sizeof(ProgramTcamAclCtl_m));
    SetProgramTcamAclCtl(V, l4SymmetricSessionEnBitmap_f, &program_acl_ctl, 0);
    SetProgramTcamAclCtl(V, fsMergeDataShareTypeCtl_f, &program_acl_ctl, 0x7);
    cmd = DRV_IOW(ProgramTcamAclCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &program_acl_ctl));
    cmd = DRV_IOW(EgrProgramTcamAclCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &program_acl_ctl));

    sal_memset(&egr_prog_acl_ctl1, 0, sizeof(EgrProgramTcamAclCtl1_m));
    SetEgrProgramTcamAclCtl1(V, aclGratuitousArpOpcodeCheckEn_f, &egr_prog_acl_ctl1, 1);    /*AT refer to FlowTcamLookupCtl_gratuitousArpOpcodeCheckEn_f*/
    SetEgrProgramTcamAclCtl1(V, aclKeyNeedMoreFragField_f, &egr_prog_acl_ctl1, 0);  /*AT use isFrag*/
    cmd = DRV_IOW(EgrProgramTcamAclCtl1_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &egr_prog_acl_ctl1));

    cmd = DRV_IOR(IpeAclQosCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &acl_qos_ctl));
    SetIpeAclQosCtl(V, modeCtl_f, &acl_qos_ctl, 0);
    cmd = DRV_IOW(IpeAclQosCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &acl_qos_ctl)); /*default use per lookup action priority, else use per ltid action priority*/

    sal_memset(&glb_config, 0, sizeof(sys_acl_league_glb_config_t));
    glb_config.op_type = SYS_ACL_LEAGUE_CONFIG_INIT;
    sys_tmm_acl_set_league_global_config(lchip, &glb_config);
    glb_config.block_id = ACL_IGS_BLOCK_MAX_NUM;
    sys_tmm_acl_set_league_global_config(lchip, &glb_config);

    cmd = DRV_IOR(EpeAclTcamSlicePriority_t, DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &epe_acl_prio);
    cmd = DRV_IOR(EpeAclLkpSelectTcamSlice_t, DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &lkup_select);
    for (loop=0; loop<MCHIP_CAP(SYS_CAP_ACL_EGRESS_LKUP_NUM); loop++)
    {
        SetEpeAclTcamSlicePriority(V, gTcamSlice_0__priority_f + loop, &epe_acl_prio, MCHIP_CAP(SYS_CAP_ACL_EGRESS_LKUP_NUM) - 1- loop);
        SetEpeAclLkpSelectTcamSlice(V, gAclLkpLevel_0_selectTcamSliceBitmap_f+loop, &lkup_select, (1<<loop));
    }
    cmd = DRV_IOW(EpeAclTcamSlicePriority_t, DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &epe_acl_prio);
    cmd = DRV_IOW(EpeAclLkpSelectTcamSlice_t, DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &lkup_select);

#if defined(ARCTIC)
    if (DRV_FROM_AT(lchip))
    {
        sys_at_map_discard_type(lchip, CTC_BOTH_DIRECTION, FALSE);
    }
#endif


    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_get_lt_block_by_pe_fpa(uint8 lchip, ctc_fpa_entry_t* pe, ctc_fpa_block_t** pb)
{
    sys_acl_presel_t* presel;

    CTC_PTR_VALID_CHECK(pe);
    CTC_PTR_VALID_CHECK(pb);

    *pb = NULL;

    presel = ctc_vector_get(SYS_ACL_PRESEL_VEC(lchip), pe->entry_id);
    if (NULL == presel)
    {
        CTC_ERROR_RETURN(CTC_E_ENTRY_NOT_EXIST);
    }

    *pb = &(presel->pltb->fpab);

    return CTC_E_NONE;
}

/*
 * move entry in hardware table to an new index.
 */
STATIC int32
_sys_tmm_acl_presel_move_hw_fpa(uint8 lchip, ctc_fpa_entry_t* p_fpa_entry, int32 tcam_idx_new)
{
    uint8   part = 0;
    uint8   block_id = 0;
    uint8   ltid = 0;
    DsLtidSelectTcam_m presel_key, presel_mask;
    DsProgramKeyGenProfile0_m profile;
    DsLtidSelectAd0_m ltad;
    tbl_entry_t tcam_key;
    sys_acl_tbl_info_t tbl_info;
    sys_acl_presel_t*  presel = NULL;
    sys_acl_lt_block_t* p_ltb = NULL;

    CTC_PTR_VALID_CHECK(p_fpa_entry);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    sys_tmm_acl_dump_fpa_entry(p_fpa_entry);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM, "tcam_idx_new- %u\n", tcam_idx_new);

    sal_memset(&tcam_key, 0, sizeof(tcam_key));
    sal_memset(&presel_key, 0, sizeof(DsLtidSelectTcam_m));
    sal_memset(&presel_mask, 0, sizeof(DsLtidSelectTcam_m));

    if (p_fpa_entry->flag != FPA_ENTRY_FLAG_INSTALLED)
    {
        return CTC_E_NONE;
    }

    /*get entry*/
    presel = ctc_vector_get(SYS_ACL_PRESEL_VEC(lchip), p_fpa_entry->entry_id);
    if (NULL == presel)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, " [Acl] Entry not exist \n");
        return CTC_E_NOT_EXIST;
    }
    p_ltb = presel->pltb;
    block_id = p_ltb->block_id;
    for (part=0; part<presel->fk->key_size; part++)
    {
        p_ltb = p_ltb + part;
        sal_memset(&tbl_info, 0, sizeof(sys_acl_tbl_info_t));
        tbl_info.block_id = block_id;
        tbl_info.part = part;
        tbl_info.op_mode = SYS_ACL_OP_READ;
        tbl_info.index = p_fpa_entry->offset_a;
        /*1.read old ad*/
        tbl_info.buffer[0] = &profile;
        tbl_info.buffer[1] = &ltad;
        _sys_tmm_acl_fk_op_hw(lchip, &ltid, &tbl_info, presel->fk);
        /*2.read old key*/
        tbl_info.buffer[0] = &presel_key;
        tbl_info.buffer[1] = &presel_mask;
        _sys_tmm_acl_presel_op_hw(lchip, &tbl_info);

        tbl_info.op_mode = SYS_ACL_OP_WRITE;
        tbl_info.index = tcam_idx_new;
        /*3.write new ad*/
        tbl_info.buffer[0] = &profile;
        tbl_info.buffer[1] = &ltad;
        _sys_tmm_acl_fk_op_hw(lchip, &ltid, &tbl_info, presel->fk);
        /*4.write new key*/
        tbl_info.buffer[0] = &presel_key;
        tbl_info.buffer[1] = &presel_mask;
        _sys_tmm_acl_presel_op_hw(lchip, &tbl_info);
        CTC_BIT_SET(p_ltb->entry_valid, tcam_idx_new);
        if(presel->fk->key_size == 1)
        {
            CTC_BIT_SET(p_ltb->is_key160, tcam_idx_new);
        }

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "Move presel %d from %s[%d] to %s[%d]\n",
                                              p_fpa_entry->entry_id,
                                              DRV_TABLE_NAME(lchip, DsLtidSelectTcam_t), p_fpa_entry->offset_a+block_id*SYS_ACL_PRESEL_TCAM_DEPTH,
                                              DRV_TABLE_NAME(lchip, DsLtidSelectTcam_t), tcam_idx_new+block_id*SYS_ACL_PRESEL_TCAM_DEPTH);
    }

    /*5.delete old key*/
    p_ltb = presel->pltb;
    for (part=0; part<presel->fk->key_size; part++)
    {
        p_ltb = p_ltb + part;
        tbl_info.op_mode = SYS_ACL_OP_DEL;
        tbl_info.part = part;
        tbl_info.index = p_fpa_entry->offset_a;
        tbl_info.buffer[0] = &presel_key;
        tbl_info.buffer[1] = &presel_mask;
        _sys_tmm_acl_presel_op_hw(lchip, &tbl_info);
        CTC_BIT_UNSET(p_ltb->entry_valid, p_fpa_entry->offset_a);
        CTC_BIT_UNSET(p_ltb->is_key160, p_fpa_entry->offset_a);
    }

    /* set new_index */
    p_fpa_entry->offset_a = tcam_idx_new;
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);

    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_presel_fpa_block_init(uint8 lchip, uint8 block_id)
{
    uint8  loop = 0;
    uint8  dir = SYS_ACL_BLOCK_DIR(block_id);
    uint8  max_block_num = SYS_ACL_BLOCK_MAX_NUM(block_id);
    uint32 size = 0;
    uint32 ltb_size = 0;
    uint32 hw_table[CTC_BOTH_DIRECTION] = {DsLtidSelectTcam_t, DsEgrLtidSelectTcam_t};
    ctc_fpa_block_t *p_lt_fpab = NULL;
    sys_acl_lt_block_t *p_ltb = NULL;

    ACL_GET_TABLE_ENTYR_NUM(lchip, hw_table[dir], &ltb_size);
    ltb_size = ltb_size / max_block_num - SYS_ACL_PRESEL_RSV_NUM;

    size = sizeof(sys_acl_presel_t*) * ltb_size;
    p_ltb = &SYS_ACL_LT_BLOCK(lchip, block_id);
    sal_memset(p_ltb, 0, sizeof(sys_acl_lt_block_t));
    p_lt_fpab = &p_ltb->fpab;
    MALLOC_ZERO(MEM_ACL_MODULE, p_lt_fpab->entries, size);
    if ((NULL == p_lt_fpab->entries) && (size != 0))
    {
        CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
    }

    //p_ltb->entry_num = ltb_size;
    p_ltb->block_id = block_id;
    for (loop=SYS_ACL_KEY_LTID_RSV_BASE; loop<=SYS_ACL_KEY_LTID_RSV_MAX; loop++)
    {
        CTC_BIT_SET(p_ltb->ltid_bitmap, loop);
    }
    p_lt_fpab->entry_count = ltb_size;
    p_lt_fpab->free_count  = ltb_size;
    p_lt_fpab->start_offset[0]    = 0;
    p_lt_fpab->sub_entry_count[0] = ltb_size;
    p_lt_fpab->sub_free_count[0]  = ltb_size;
    p_lt_fpab->sub_rsv_count[0]   = 0;
    p_lt_fpab->part_num = 1;

    return CTC_E_NONE;
}

STATIC int32 _sys_tmm_acl_presel_expand_lt_block(uint8 lchip, uint8 block_id, uint8 key_size)
{
    uint8  old_part_num = 0; 
    uint8  part = 0;
    sys_acl_lt_block_t *p_ltb = NULL;

    SYS_ACL_PART_LOOP(part, key_size)
    {
        p_ltb = &SYS_ACL_LT_BLOCK(lchip, block_id+part);

        old_part_num = p_ltb->fpab.part_num; 
        
        if(key_size > old_part_num)
        {
            p_ltb->fpab.part_num = key_size;
            p_ltb->fpab.part     = part;
        }    
    }

    return CTC_E_NONE;
}

STATIC int32 _sys_tmm_acl_presel_compress_lt_block(uint8 lchip, uint8 block_id, uint8 key_size, uint16 presel_id)
{
    sys_acl_lt_block_t *p_ltb = NULL;

    uint8  old_part_num = 0;
    uint8  index = 0;
    uint8  max_part_num = 0;
    uint8  entry_key_size = 0;
    uint8  part = 0;

    p_ltb = &SYS_ACL_LT_BLOCK(lchip, block_id);

    old_part_num = p_ltb->fpab.part_num;

    /* if delete presel entry key size less than part_num, do nothing */
    if(key_size < old_part_num)
    {
        return CTC_E_NONE;
    }

    /* get max part_num from remain entries */
    for (index=0; index<SYS_ACL_KEY_LTID_RSV_BASE; index++)
    {
        SYS_ACL_IF_CONTINUE(p_ltb->fpab.entries[index] == NULL);
        SYS_ACL_IF_CONTINUE(p_ltb->fpab.entries[index]->entry_id == presel_id);/* except delete presel */

        entry_key_size =  p_ltb->fpab.entries[index]->key_size;

        max_part_num = max_part_num > entry_key_size ? max_part_num : entry_key_size;
    }   

    /* update part_num and part of every unuse lt blocks */
    if(max_part_num>0 && max_part_num < key_size)
    {
        SYS_ACL_PART_LOOP(part, key_size)
        {
            if(part < max_part_num)
            {
                p_ltb[part].fpab.part_num = max_part_num;/* update value */
            }
            else
            {
                p_ltb[part].fpab.part_num = 1;/* init value */
                p_ltb[part].fpab.part     = 0;
            }
        }
    }
    else if(max_part_num == 0)/* have no other entry */
    {
        SYS_ACL_PART_LOOP(part, key_size)
        {
            p_ltb[part].fpab.part_num = 1;/* init value */
            p_ltb[part].fpab.part     = 0;
        }
    }
    
    return CTC_E_NONE;
}


int32
sys_tmm_acl_op_glb_presel_en(uint8 lchip, uint8 is_set, uint8* enable)
{
    if (enable)
    {
        *enable = p_usw_acl_master[lchip]->glb_presel_en ? 1 : 0;
    }
    else
    {
        if(!is_set != !p_usw_acl_master[lchip]->glb_presel_en)
        {
            if(p_usw_acl_master[lchip]->presel_vec->used_cnt)
            {
                return CTC_E_IN_USE;
            }
            SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_MASTER, 1);
        }
        p_usw_acl_master[lchip]->glb_presel_en = is_set ? 1 : 0;

    }
    return CTC_E_NONE;
}

STATIC int32
_sys_at_acl_support_kset_init(uint8 lchip)
{
#define SYS_ACL_DEF_K(DIR)      p_kset = &support_kset[DIR];
#define SYS_ACL_DEF_F(FIELD)    CTC_BMP_SET(p_kset->w, FIELD);

    ctc_acl_kset_t* support_kset = NULL;
    ctc_acl_kset_t* p_kset = NULL;
    uint8 num = 0;

    num = CTC_BOTH_DIRECTION + CTC_ACL_KEY_HASH_NSH-CTC_ACL_KEY_HASH_MAC+1;/* support flew hash key type*/
    support_kset = mem_malloc(MEM_ACL_MODULE, num*sizeof(ctc_acl_kset_t));
    if (support_kset == NULL)
    {
        return CTC_E_NO_MEMORY;
    }
    sal_memset(support_kset, 0, num*sizeof(ctc_acl_kset_t));
    #include "../arctic/sys_at_acl_kset.h"

    /* sys_at_acl_kset.h is global cfg for tm2/at/..., some fields is not same of diff chip, so unset due to special chip */
    if (DRV_IS_AT(lchip))
    {
        SYS_ACL_PKSET_ADD(&support_kset[CTC_EGRESS], CTC_FIELD_KEY_NPM_IM_FLOW_ID);
        SYS_ACL_PKSET_ADD(&support_kset[CTC_EGRESS], CTC_FIELD_KEY_NPM_IM_L_FLAG);
        SYS_ACL_PKSET_ADD(&support_kset[CTC_EGRESS], CTC_FIELD_KEY_NPM_IM_D_FLAG);
    }

    p_usw_acl_master[lchip]->support_kset = support_kset;
    return CTC_E_NONE;
}

STATIC int32
_sys_at_acl_entry_pp_hash_init(uint8 lchip)
{
    uint8 pp_base = drv_vchip_get_pp_base(lchip);
    uint8 type = 0;
    uint32 entry_num[2] = {SYS_ACL_USW_ENTRY_HASH_SIZE, MCHIP_CAP(SYS_CAP_ACL_UDF_ENTRY_NUM)};
    uint32 block_size[2] = { CTC_HASH_1K_BLOCK_SIZE, CTC_HASH_16_BLOCK_SIZE};
    int32 ret = CTC_E_NONE;
    void* p_hash = NULL;

    if (SYS_COM_ENTRY_HASH_SUPPORT_CHECK(lchip, CTC_FEATURE_ACL))
    {
        return CTC_E_NONE;
    }
    if (lchip != pp_base)
    {
        for(type = 0; type <= SYS_COM_ENTRY_HASH_ACL_UDF_ENTRY; type++)
        {
            p_usw_acl_master[lchip]->entry_hash_db[type] =  p_usw_acl_master[pp_base]->entry_hash_db[type];
        }
        return CTC_E_NONE;
    }

    type = 0;
    do{
        p_hash = sys_usw_com_entry_hash_init(lchip, entry_num[type], block_size[type]);
        if(!p_hash)
        {
            ret = CTC_E_NO_MEMORY;
            goto done;
        }
        p_usw_acl_master[lchip]->entry_hash_db[type] = p_hash;
    }while(type++ < SYS_COM_ENTRY_HASH_ACL_UDF_ENTRY);
    
    return ret;
done:
    sys_usw_com_entry_hash_deinit(lchip, p_usw_acl_master[lchip]->entry_hash_db[SYS_COM_ENTRY_HASH_ACL_ENTRY]);
    return ret;
}

STATIC void
_sys_at_acl_entry_pp_hash_deinit(uint8 lchip)
{
    uint8 type = 0;
    if (SYS_VCHIP_PP_IS_NOT_BASE(lchip))
    {
        for(type = 0; type < SYS_COM_ENTRY_HASH_ACL_UDF_ENTRY; type++)
        {
            p_usw_acl_master[lchip]->entry_hash_db[type] =  NULL;
        }
        return;
    }

    sys_usw_com_entry_hash_deinit(lchip, p_usw_acl_master[lchip]->entry_hash_db[SYS_COM_ENTRY_HASH_ACL_ENTRY]);
    sys_usw_com_entry_hash_deinit(lchip, p_usw_acl_master[lchip]->entry_hash_db[SYS_COM_ENTRY_HASH_ACL_UDF_ENTRY]);
    return ;
}

void
_sys_tmm_acl_def_presel_init(uint8 lchip)
{
    uint8 key_type = 0;
    uint8 dir = 0;
    uint8 block_id = 0;
    uint8 block_num[CTC_BOTH_DIRECTION];
    sys_acl_flex_key_t* fk = NULL;
    sys_acl_ext_sel_t* p_sel = NULL;
    uint32 profile_table[CTC_BOTH_DIRECTION] = {DsProgramKeyGenProfile0_t, DsEgrProgramKeyGenProfile0_t};
    DsProgramKeyGenProfile_m  profile;
    uint32 cmd = 0;

    block_num[CTC_INGRESS] = ACL_IGS_BLOCK_MAX_NUM;
    block_num[CTC_EGRESS]  = ACL_EGS_BLOCK_MAX_NUM;
    
    for(key_type = 0; key_type < CTC_ACL_KEY_FLEX_BASE; key_type++)
    {
        dir = 0;
        do{
            fk = sys_tmm_acl_get_flex_key(lchip, dir, key_type);
            if(fk == NULL)
            {
                continue;
            }
            p_sel = &(fk->ext_codes[0]);
            sal_memset(&profile, 0, sizeof(DsProgramKeyGenProfile_m));
            SetDsProgramKeyGenProfile(V, gL0Extractor8b_0_selFieldId_f + SYS_ACL_EXT_L1_E8_MAX - 1, &profile, p_sel->l1_e8_sel[SYS_ACL_EXT_L1_E8_MAX - 1]);
            SetDsProgramKeyGenProfile(V, gL2Extractor1b_0_selFieldId_f + SYS_ACL_EXT_L3_E1_MAX - 1, &profile, p_sel->l3_e1_sel[SYS_ACL_EXT_L3_E1_MAX - 1]);
            SetDsProgramKeyGenProfile(V, gL2Extractor4b_0_selFieldId_f + SYS_ACL_EXT_L3_E4_MAX - 1, &profile, p_sel->l3_e4_sel[SYS_ACL_EXT_L3_E4_MAX - 1]);
            /*LTID*/
            SYS_ACL_IGS_BLK_LOOP(block_id, block_num[dir])
            {
                cmd = DRV_IOW(profile_table[dir]+block_id, DRV_ENTRY_FLAG);
                DRV_IOCTL(lchip, fk->ltid[0], DRV_CMD_BMP_EN(cmd, (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) )), &profile);
            }
        }while(dir++ < CTC_EGRESS);
    }
    return;
}

int32
sys_tmm_acl_init(uint8 lchip)
{
    uint8  pp_base = 0;
    uint8  block_id = 0;
    uint8  acl_block_num = ACL_IGS_BLOCK_MAX_NUM;
    uint32 ceth_type_size = 0;
    ctc_spool_t spool;
    sys_usw_opf_t opf;

    sal_memset(&opf, 0, sizeof(opf));
    CTC_ERROR_RETURN(sys_usw_ftm_query_table_entry_num(lchip, ProgramAclEtherTypeCompressor_t, &ceth_type_size));
    CTC_ERROR_RETURN(SYS_CB_REGISTER(lchip, SYS_CB_ACL_OP_GLB_PRESEL_EN, sys_tmm_acl_op_glb_presel_en));

    CTC_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_PORT);
    CTC_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_VLAN);
    CTC_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_L3IF);
    p_usw_acl_master[lchip]->egs_league_update_bmp = p_usw_acl_master[lchip]->league_update_bmp;
    if (DRV_FROM_AT(lchip))
    {
        acl_block_num = ACL_IGS_BLOCK_MAX_NUM + ACL_EGS_BLOCK_MAX_NUM + ACL_XLOOKUP_BLOCK_MAX_NUM;
    }
    SYS_ACL_FLEX_KEY_VEC(lchip) = ctc_vector_init(CTC_BOTH_DIRECTION*SYS_ACL_FLEX_KEY_VEC_BLK_NUM, SYS_ACL_FLEX_KEY_VEC_BLK_SIZE);
    SYS_ACL_DFT_FLEX_KEY_VEC(lchip) = ctc_vector_init(CTC_BOTH_DIRECTION, SYS_ACL_FLEX_KEY_VEC_BLK_SIZE);

    pp_base = drv_vchip_get_pp_base(lchip);
    if (DRV_FROM_AT(lchip) && lchip != pp_base)
    {
        p_usw_acl_master[lchip]->kgn_ext_cfg_db_arr[SYS_ACL_KEYGEN_MODE_SINGLE] = p_usw_acl_master[pp_base]->kgn_ext_cfg_db_arr[SYS_ACL_KEYGEN_MODE_SINGLE];
        p_usw_acl_master[lchip]->kgn_ext_cfg_db_arr[SYS_ACL_KEYGEN_MODE_DOUBLE] = p_usw_acl_master[pp_base]->kgn_ext_cfg_db_arr[SYS_ACL_KEYGEN_MODE_DOUBLE];
        p_usw_acl_master[lchip]->kgn_ext_cfg_db_arr[SYS_ACL_KEYGEN_MODE_TRIPLE] = p_usw_acl_master[pp_base]->kgn_ext_cfg_db_arr[SYS_ACL_KEYGEN_MODE_TRIPLE];
        p_usw_acl_master[lchip]->kgn_field_cfg_info_db = p_usw_acl_master[pp_base]->kgn_field_cfg_info_db;
    }
    else
    {
        CTC_ERROR_RETURN(sys_tmm_acl_extract_db_init(lchip));
    }
    CTC_ERROR_RETURN(_sys_tmm_acl_flex_key_init(lchip));

    if (!CTC_WB_ENABLE(lchip) || CTC_WB_STATUS(lchip) != CTC_WB_STATUS_RELOADING)
    {
        _sys_tmm_acl_def_presel_init(lchip);
    }
    SYS_ACL_PRESEL_VEC(lchip) = ctc_vector_init(acl_block_num, SYS_ACL_FLEX_KEY_VEC_BLK_SIZE);
    p_usw_acl_master[lchip]->presel_fpa = fpa_usw_create(lchip,
                                                         _sys_tmm_acl_get_lt_block_by_pe_fpa,
                                                         _sys_tmm_acl_presel_move_hw_fpa,
                                                         0);
    CTC_ERROR_RETURN(_sys_tmm_acl_reg_init(lchip));
    CTC_ERROR_RETURN(_sys_at_acl_entry_pp_hash_init(lchip));

    for (block_id = 0; block_id < ACL_IGS_BLOCK_MAX_NUM; block_id++)
    {
        CTC_ERROR_RETURN(_sys_tmm_acl_presel_fpa_block_init(lchip, block_id));
        CTC_ERROR_RETURN(_sys_tmm_acl_fk_write_action_priority(lchip, block_id, 0, ACL_IGS_BLOCK_MAX_NUM -block_id));
    }
    if (DRV_FROM_AT(lchip))
    {
        for (block_id = 0; block_id < ACL_EGS_BLOCK_MAX_NUM; block_id++)
        {
            CTC_ERROR_RETURN(_sys_tmm_acl_presel_fpa_block_init(lchip, block_id+ACL_IGS_BLOCK_MAX_NUM));
            CTC_ERROR_RETURN(_sys_tmm_acl_fk_write_action_priority(lchip, block_id+ACL_IGS_BLOCK_MAX_NUM, 0, ACL_EGS_BLOCK_MAX_NUM -block_id));
        }
    }

    /*spool for compress ether type profile*/
    sal_memset(&spool, 0, sizeof(ctc_spool_t));
    spool.lchip = lchip;
    spool.block_num = 1;
    spool.block_size = ceth_type_size;
    spool.max_count = ceth_type_size;
    spool.user_data_size = sizeof(sys_register_cethertype_t);
    spool.desc = "opf-acl-cethertype";
    spool.key_size = CTC_OFFSET_OF(sys_register_cethertype_t, calc_len);
    spool.min_index = 1;
    spool.max_index = (DRV_FROM_AT(lchip) && MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_ACL)) ? (ceth_type_size/2 - 1) : (ceth_type_size - 1);
    p_usw_acl_master[lchip]->cethertype_spool[0][CTC_INGRESS] = ctc_spool_create(&spool);
    if (!p_usw_acl_master[lchip]->cethertype_spool[0][CTC_INGRESS])
    {
        return CTC_E_INIT_FAIL;
    }
    p_usw_acl_master[lchip]->cethertype_spool[0][CTC_EGRESS] = ctc_spool_create(&spool);
    if (!p_usw_acl_master[lchip]->cethertype_spool[0][CTC_EGRESS])
    {
        return CTC_E_INIT_FAIL;
    }
    /* glocal acl init resource */
    if(DRV_FROM_AT(lchip) && MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_ACL))
    {
        spool.min_index = ceth_type_size/2;
        spool.max_index = ceth_type_size - 1;
        p_usw_acl_master[lchip]->cethertype_spool[1][CTC_INGRESS] = ctc_spool_create(&spool);
        if (!p_usw_acl_master[lchip]->cethertype_spool[1][CTC_INGRESS])
        {
            return CTC_E_INIT_FAIL;
        }
        p_usw_acl_master[lchip]->cethertype_spool[1][CTC_EGRESS] = ctc_spool_create(&spool);
        if (!p_usw_acl_master[lchip]->cethertype_spool[1][CTC_EGRESS])
        {
            return CTC_E_INIT_FAIL;
        }
    }
    p_usw_acl_master[lchip]->build_key_func[CTC_ACL_KEY_HASH_NSH] = (SYS_CB_ACL_BUILD_KEY_FUNC_T)sys_at_acl_add_hash_nsh_key_field;
    p_usw_acl_master[lchip]->set_flex_key_fields[0] = sys_tmm_acl_set_flex_key_fields;
    
 #if defined(ARCTIC)
    p_usw_acl_master[lchip]->set_flex_key_fields[1] = sys_at_acl_set_flex_hash_fields;
 #endif
    _sys_at_acl_support_kset_init(lchip);
    if(DRV_FROM_TMM(lchip))
    {
        p_usw_acl_master[lchip]->nhid_hash = ctc_hash_create(SYS_ACL_NHID_HASH_NUM/CTC_HASH_1K_BLOCK_SIZE, CTC_HASH_1K_BLOCK_SIZE,
                                                        (hash_key_fn)sys_usw_ldp_hash_make,
                                                        (hash_cmp_fn)sys_usw_ldp_hash_cmp);
        if (!p_usw_acl_master[lchip]->nhid_hash)
        {
            return CTC_E_NO_MEMORY;
        }

        p_usw_acl_master[lchip]->ldp_entry = ctc_hash_create(SYS_ACL_USW_ENTRY_HASH_SIZE/CTC_HASH_1K_BLOCK_SIZE,
                                                            CTC_HASH_1K_BLOCK_SIZE,
                                                            (hash_key_fn) _sys_usw_acl_make_ldp_entry,
                                                            (hash_cmp_fn) _sys_usw_acl_compare_ldp_entry);
        if (!p_usw_acl_master[lchip]->ldp_entry)
        {
            return CTC_E_NO_MEMORY;
        }
    }
    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_free_fk_node_data(void* array_data, void* user_data)
{
    uint8  part = 0, part_num = 0;
    sys_acl_flex_key_t *fk = array_data;

    if (NULL != array_data)
    {
        if (!SYS_ACL_FK_IS_HASH(fk))
        {
            part_num = fk->key_size;
            SYS_ACL_PART_LOOP(part, part_num)
            {
                mem_free(fk->field_arr[part].qid_arr);
                mem_free(fk->field_arr[part].offset_arr);
            }
        }
        mem_free(array_data);
    }
    return CTC_E_NONE;
}
STATIC int32
_sys_tmm_acl_replace_update_flex_key(void* array_data, uint32 key_index, void* user_data)
{
    sys_acl_flex_key_t *fk = array_data;
    uint8 part = 0;
    uint8 lchip = ((*(uint32*)user_data)>>24)&0xFF;
    uint8 src_block = (*(uint32*)user_data)&0xFF;
    uint8 dest_block = ((*(uint32*)user_data)>>8)&0xFF;
    uint8 part_num = ((*(uint32*)user_data)>>16)&0xFF;
    uint8 fk_part_num = 0;
    uint8 dir = SYS_ACL_BLOCK_DIR(src_block);
    uint8 block_base = SYS_ACL_BLOCK_BASE(src_block);
    uint8 key_type = 0;
    uint8 tmp_dir = 0;

    SYS_ACL_KEY_SIZE_TO_PART_NUM(fk->key_size, fk_part_num);
    SYS_ACL_DECODE_FLEX_KEY_IDX(key_index, tmp_dir, key_type);
    if(tmp_dir != dir || fk_part_num > part_num)
    {
        return CTC_E_NONE;
    }
    src_block = src_block - block_base;
    dest_block = dest_block - block_base;
    SYS_ACL_PART_LOOP(part, fk_part_num)
    {
        fk->entry_count[dest_block + part] = fk->entry_count[src_block+ part];
        fk->entry_count[src_block + part] = 0;

        if (!p_usw_acl_master[lchip]->glb_presel_en)
        {
            if(CTC_IS_BIT_SET(fk->presel_cnt, src_block))
            {
                CTC_BIT_UNSET(fk->presel_cnt, src_block);
                CTC_BIT_SET(fk->presel_cnt, dest_block);
            }
        }
        if(SYS_ACL_KEY_IS_RSVD(key_type))
        {
            continue;
        }

        /* global presel cover all block, so do not update params below */
        if (!p_usw_acl_master[lchip]->glb_presel_en)
        {            
            fk->ltid[dest_block + part] = fk->ltid[src_block + part];
            fk->ltid[src_block + part] = 0;
            if(CTC_IS_BIT_SET(fk->ltid_valid, src_block + part))
            {
                CTC_BIT_UNSET(fk->ltid_valid, (src_block + part));
                CTC_BIT_SET(fk->ltid_valid, (dest_block + part));
            }
        }
    }
    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_free_presel_vec_node_data(void* node_data, void* user_data)
{
    sys_acl_presel_t *presel = node_data;
    if (!node_data)
    {
        return CTC_E_NONE;
    }
    if (NULL != presel->buffer)
    {
        mem_free(presel->buffer);
    }
    mem_free(node_data);
    return CTC_E_NONE;
}

void
sys_tmm_acl_deinit(uint8 lchip)
{
    uint8  block_id = 0;
    sys_acl_lt_block_t *p_ltb = NULL;
    if (p_usw_acl_master[lchip]->cethertype_spool[0][CTC_INGRESS])
    {
        ctc_spool_free(p_usw_acl_master[lchip]->cethertype_spool[0][CTC_INGRESS]);
    }
    if (p_usw_acl_master[lchip]->cethertype_spool[0][CTC_EGRESS])
    {
        ctc_spool_free(p_usw_acl_master[lchip]->cethertype_spool[0][CTC_EGRESS]);
    }
    if (p_usw_acl_master[lchip]->cethertype_spool[1][CTC_INGRESS])
    {
        ctc_spool_free(p_usw_acl_master[lchip]->cethertype_spool[1][CTC_INGRESS]);
    }
    if (p_usw_acl_master[lchip]->cethertype_spool[1][CTC_EGRESS])
    {
        ctc_spool_free(p_usw_acl_master[lchip]->cethertype_spool[1][CTC_EGRESS]);
    }
    for (block_id = 0; block_id < (ACL_IGS_BLOCK_MAX_NUM+ACL_EGS_BLOCK_MAX_NUM); block_id++)
    {
        p_ltb = &p_usw_acl_master[lchip]->lt_block[block_id];
        mem_free(p_ltb->fpab.entries);
    }
    if (lchip == drv_vchip_get_pp_base(lchip))
    {
        sys_tmm_acl_extract_db_deinit(lchip);
    }

    ctc_vector_traverse(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), (vector_traversal_fn)_sys_tmm_acl_free_fk_node_data, NULL);
    ctc_vector_release(SYS_ACL_DFT_FLEX_KEY_VEC(lchip));
    ctc_vector_traverse(SYS_ACL_FLEX_KEY_VEC(lchip), (vector_traversal_fn)_sys_tmm_acl_free_fk_node_data, NULL);
    ctc_vector_release(SYS_ACL_FLEX_KEY_VEC(lchip));

    fpa_usw_free(p_usw_acl_master[lchip]->presel_fpa);
    ctc_vector_traverse(SYS_ACL_PRESEL_VEC(lchip), (vector_traversal_fn)_sys_tmm_acl_free_presel_vec_node_data, NULL);
    ctc_vector_release(SYS_ACL_PRESEL_VEC(lchip));
    mem_free(p_usw_acl_master[lchip]->support_kset);
    ctc_hash_free2(p_usw_acl_master[lchip]->nhid_hash, (hash_traversal_fn)sys_usw_free_ldp_node, NULL);
    ctc_hash_free2(p_usw_acl_master[lchip]->ldp_entry, (hash_traversal_fn)sys_usw_free_ldp_node, NULL);
    _sys_at_acl_entry_pp_hash_deinit(lchip);
}

#define SYS_USW_ACL_IPFIX_CONFLICT_CHECK(ipfix_op_code, p_buffer) \
    do{\
        uint8 tmp_ipfix_op_code = GetDsEpeAcl(V, ipfixOpCode_f, p_buffer->action);\
        if (tmp_ipfix_op_code && (ipfix_op_code != tmp_ipfix_op_code))\
        {\
            CTC_ERROR_RETURN(CTC_E_PARAM_CONFLICT);\
        }\
    }while(0)
#define SYS_USW_ACL_CHECK_ACTION_CONFLICT(action_bmp,conf_action0,conf_action1, conf_action2) \
    do{\
        if(conf_action0 && CTC_BMP_ISSET(action_bmp, conf_action0))\
        {\
            return CTC_E_PARAM_CONFLICT;\
        }\
        if(conf_action1 && CTC_BMP_ISSET(action_bmp, conf_action1))\
        {\
            return CTC_E_PARAM_CONFLICT;\
        }\
        if(conf_action1 && CTC_BMP_ISSET(action_bmp, conf_action2))\
        {\
            return CTC_E_PARAM_CONFLICT;\
        }\
    }while(0)
#define SYS_USW_ACL_CHECK_ACTION_POLICER_MODE(pe,_policer_mode) \
    do{\
        if(0!=pe->policer_mode && pe->policer_mode != (_policer_mode))\
        {\
            return CTC_E_PARAM_CONFLICT;\
        }\
    }while(0)



int32
sys_tmm_acl_add_egs_action_field(uint8 lchip, ctc_acl_field_action_t* action_field, void* pe_info, uint8 is_add)
{
    uint32  data0        = 0;
    uint32  data1        = 0;
    uint32  stats_ptr    = 0;
    uint8   profile_id   = 0;
    uint8   do_vlan_edit = 0;
    uint8   temp_u2_type = 0;
    ctc_acl_vlan_edit_t*    p_ctc_vlan_edit = NULL;
    sys_acl_vlan_edit_t     vlan_edit;
    sys_acl_buffer_t*       p_buffer = NULL;
    ctc_acl_to_cpu_t*       p_cp_to_cpu = NULL;
    ctc_acl_oam_t*          p_oam = NULL;
    sys_qos_policer_param_t policer_param,policer_grp_param;
    sys_qos_policer_group_info_t policer_group_info;
    sys_cpu_reason_info_t   reason_info;
    sys_com_dot1ae_bind_sc_t dot1ae_bind_sc;
    sys_acl_entry_t* pe  = NULL;
    uint8   stats_pool_num = 0;
    uint8 no_aset = 1;
    uint8 im_flow_valid = 0;
    uint8 is_glb_entry  = 0;
    uint32 im_flow_id = 0;
    sys_npm_im_flow_hash_t npm_im_flow;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    pe = (sys_acl_entry_t *) pe_info;
    no_aset = SYS_ACL_ASET_IS_DEF(pe->aset_id);

    p_buffer = &pe->ua->buffer;
    sal_memset(&vlan_edit, 0, sizeof(sys_acl_vlan_edit_t));
    sal_memset(&policer_param, 0, sizeof(sys_qos_policer_param_t));
    sal_memset(&policer_grp_param, 0, sizeof(sys_qos_policer_param_t));
    sal_memset(&policer_group_info, 0, sizeof(sys_qos_policer_group_info_t));
    sal_memset(&reason_info, 0, sizeof(sys_cpu_reason_info_t));
    sal_memset(&npm_im_flow, 0, sizeof(npm_im_flow));

    if (CTC_ACL_FIELD_ACTION_NPM_IM_FLOW_ID == action_field->type)
    {
        CTC_MAX_VALUE_CHECK(action_field->data0, 0xFFFFF);
        if(is_add)
        {
            CTC_ERROR_RETURN(SYS_CB_RET(CTC_E_NOT_INIT,SYS_CB_NPM_IM_FLOW_TO_ACL, lchip, action_field->data0, (void *)&npm_im_flow));
            sys_tmm_flow_stats_get_acl_stats_ram_id(lchip, npm_im_flow.egs_stats_ptr + pe->im_l_flag, CTC_EGRESS, &stats_pool_num, &stats_ptr);
        }
        //p_buffer->ad.tcam.im_flow_id = action_field->data0;
        im_flow_id = action_field->data0;
        action_field->type = CTC_ACL_FIELD_ACTION_STATS;
        action_field->data0 = is_add?stats_ptr:0;
        action_field->data1 = 1;
        im_flow_valid = 1;
    }

    if (is_add)
    {
        data0 = action_field->data0;
        data1 = action_field->data1;
    }

    switch(action_field->type)
    {
    case CTC_ACL_FIELD_ACTION_XDATA:
    {
        uint8  loop = 0;
        uint32 chunk_bmp = 0;
        uint32 xdata[3]={0};
        ctc_xdata_t p_xdata;
        ctc_xdata_field_t fields[9];
        ctc_xdata_field_t* p_field;

        sal_memset(&p_xdata, 0, sizeof(ctc_xdata_t));
        sal_memset(fields, 0, sizeof(fields));
        CTC_ERROR_RETURN(DRV_FROM_AT(lchip)? CTC_E_NONE: CTC_E_NOT_SUPPORT);
        if(SYS_ACL_ASET_IS_DEF(pe->aset_id))
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, " Data need aset! \n");
            return CTC_E_INVALID_CONFIG;
        }

        /*1. init prof_info*/
        if(is_add)
        {
            CTC_PTR_VALID_CHECK(action_field->ext_data);
            sal_memcpy(&p_xdata, action_field->ext_data, sizeof(ctc_xdata_t));
            sal_memcpy(fields, p_xdata.fields, p_xdata.num * sizeof(ctc_xdata_field_t));
            p_xdata.fields = fields;
            if(p_xdata.prof_id != pe->ua->buffer.aset->xdata_prof_id || p_xdata.type != CTC_XDATA_PROF_TYPE_EGS_DSACL)
            {
                return CTC_E_INVALID_PARAM;
            }
        }
        else
        {
            p_xdata.fields = fields;
            p_xdata.prof_id = GetDsEpeAcl(V, xAdIndex_f, p_buffer->action);
            p_xdata.type = CTC_XDATA_PROF_TYPE_EGS_DSACL;
            if(!GetDsEpeAcl(V, xAdEn_f, p_buffer->action))
            {
                return CTC_E_NONE;
            }
        }

        /*2. get prof_info*/
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->prof_get_info(lchip, &p_xdata , is_add));

        /*3.get 68bits xAdData bus*/
        GetDsEpeAcl(A, xAdData_f, p_buffer->action, xdata);

        /*4.build xAdData*/
        for(loop=0; loop<p_xdata.num; loop++)
        {
            p_field = p_xdata.fields+loop;
            if(CTC_FLAG_ISSET(p_field->flag, CTC_XDATA_FIELD_FLAG_UDF))
            {
                continue;
            }
            CTC_MAX_VALUE_CHECK(p_field->data, (1<<p_field->width)-1);
            sys_usw_acl_offset_map_chunk(p_field, pe->group->group_info.dir, &chunk_bmp, 0);
            SYS_USW_SET_XDATA(p_field->width, p_field->offset, p_field->data, xdata);
        }

        /*5.write xAdData and chunk condition*/
        SetDsEpeAcl(A, xAdData_f, p_buffer->action, xdata);
        SetDsEpeAcl(V, xAdIndex_f, p_buffer->action, is_add ? p_xdata.prof_id : 0);
        SetDsEpeAcl(V, xAdEn_f, p_buffer->action, is_add ? 1 : 0);
        if(CTC_FLAG_ISSET(chunk_bmp, SYS_ACL_XDATA_EGS_CHUNK1_NIBBLE3_0_BYTE0))
        {
            SetDsEpeAcl(V, u1Type_f, p_buffer->action, is_add ? 1:0);
            SetDsEpeAcl(V, u1_gOam_xAdDataValid_f, p_buffer->action, is_add ? 1:0);
        }
        if(CTC_FLAG_ISSET(chunk_bmp, SYS_ACL_XDATA_EGS_CHUNK2_BYTE1_WORD0))
        {
            SetDsEpeAcl(V, u2Type_f, p_buffer->action, 0);
        }
        break;
    }
    case CTC_ACL_FIELD_ACTION_IPFIX:
    {
        ctc_acl_ipfix_t* p_ipfix = NULL;
        uint8  lantency_en_invalid = 0;

        if(!DRV_FROM_AT(lchip)) 
        {
            return CTC_E_NOT_SUPPORT;
        }

        SYS_CHECK_UNION_BITMAP(pe->u2_type,SYS_AD_UNION_G_3);
        SYS_USW_ACL_IPFIX_CONFLICT_CHECK(1, p_buffer);
        if(is_add)
        {
            CTC_PTR_VALID_CHECK(action_field->ext_data);
            p_ipfix = (ctc_acl_ipfix_t*)(action_field->ext_data);
            if(p_ipfix->hash_type == IPFIX_HASH_LKUP_TYPE_UDF || p_ipfix->hash_type == IPFIX_HASH_LKUP_TYPE_UDF_SINGLE)
            {
                return CTC_E_NOT_SUPPORT;
            }
            lantency_en_invalid |= (CTC_INGRESS == pe->group->group_info.dir && p_ipfix->lantency_en);
            CTC_ERROR_RETURN(lantency_en_invalid? CTC_E_NOT_SUPPORT: CTC_E_NONE);
            SetDsEpeAcl(V, u2Type_f, p_buffer->action, 1);
            SetDsEpeAcl(V, ipfixOpCode_f, p_buffer->action, 1);
            SetDsEpeAcl(V, u2_gIpfix_ipfixCfgProfileId_f, p_buffer->action, p_ipfix->flow_cfg_id);
            SetDsEpeAcl(V, u2_gIpfix_ipfixHashFieldSel_f, p_buffer->action, p_ipfix->field_sel_id);
            SetDsEpeAcl(V, u2_gIpfix_ipfixHashType_f, p_buffer->action, p_ipfix->hash_type);
            SetDsEpeAcl(V, u2_gIpfix_ipfixUsePIVlan_f, p_buffer->action, p_ipfix->use_mapped_vlan);
            SetDsEpeAcl(V, u2_gIpfix_egrIpfixAdUseMeasureLantency_f, p_buffer->action, p_ipfix->lantency_en? 1: 0);
            SetDsEpeAcl(V, u2_gIpfix_ipfixXKeyEn_f, p_buffer->action, p_ipfix->flex_key_en? 1: 0);
            SetDsEpeAcl(V, u2_gIpfix_denyIpfixInsertOperation_f, p_buffer->action, p_ipfix->cancel_learning? 1: 0); /*AT*/
        }
        else
        {
             /*clear u2*/
            SetDsEpeAcl(V, u2Type_f, p_buffer->action, 0);
            SetDsEpeAcl(V, ipfixOpCode_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u2_data_f, p_buffer->action, 0);
        }
        SYS_SET_UNION_TYPE(pe->u2_type,SYS_AD_UNION_G_3,DsEpeAcl,2,p_buffer->action, is_add);
        break;
    }
    case CTC_ACL_FIELD_ACTION_IPFIX_HASH_SALT:
    {
        if(!DRV_FROM_AT(lchip))
        {
            return CTC_E_NOT_SUPPORT;
        }
        SYS_CHECK_UNION_BITMAP(pe->u1_type, SYS_AD_UNION_G_3);
        if (is_add)
        {
            CTC_MAX_VALUE_CHECK(action_field->data0, 0xfff);
            SetDsEpeAcl(V, u1_gOam_ipfixHashKeySaltEn_f, p_buffer->action, 1);
            SetDsEpeAcl(V, u1_gOam_xAdData_f, p_buffer->action, (action_field->data0 & 0xfff));
        }
        else
        {
            SetDsEpeAcl(V, u1_gOam_ipfixHashKeySaltEn_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u1_gOam_xAdData_f, p_buffer->action, 0);
        }
        SYS_SET_UNION_TYPE(pe->u1_type, SYS_AD_UNION_G_3, DsAcl, 1, p_buffer->action, is_add);
        SetDsEpeAcl(V, u1Type_f, p_buffer->action, pe->u1_type == SYS_AD_UNION_G_3 ? 1 : 0);
        break;
    }
    case CTC_ACL_FIELD_ACTION_CANCEL_IPFIX_LEARNING:
        if(!DRV_FROM_AT(lchip))
        {
            return CTC_E_NOT_SUPPORT;
        }
        SYS_USW_ACL_IPFIX_CONFLICT_CHECK(3, p_buffer);
        SetDsEpeAcl(V, ipfixOpCode_f, p_buffer->action, is_add?3:0);
        break;
    case CTC_ACL_FIELD_ACTION_DOT1AE_CHAN_ID:
    {
        if (!SYS_CB_PTR(SYS_CB_DOT1AE_GET_BIND_SEC_CHAN)
            ||DRV_FROM_TMG(lchip))
        {
            return CTC_E_NOT_SUPPORT;
        }
        CTC_MAX_VALUE_CHECK(action_field->data1, MCHIP_CAP(SYS_CAP_DOT1AE_CLEAR_TAG_TYPE_MAX));
        sal_memset(&dot1ae_bind_sc, 0, sizeof(dot1ae_bind_sc));
        if(is_add)
        {
            dot1ae_bind_sc.chan_id = action_field->data0;
            CTC_ERROR_RETURN(SYS_CB(SYS_CB_DOT1AE_GET_BIND_SEC_CHAN, lchip, &dot1ae_bind_sc));
            if (!dot1ae_bind_sc.clear_tag_en && action_field->data1)
            {
                CTC_ERROR_RETURN(CTC_E_INVALID_PARAM);
            }
        }
        SetDsEpeAcl(V, gMacSec_isAdvancedMacsec_f, p_buffer->action, is_add? 1: 0);
        SetDsEpeAcl(V, gMacSec_secTagOffsetType_f, p_buffer->action, action_field->data1);
        SetDsEpeAcl(V, gMacSec_transmitScId_f, p_buffer->action, dot1ae_bind_sc.sc_index);
        //p_buffer->ad.tcam.dot1ae_chan_id = dot1ae_bind_sc.chan_id;
        break;
    }

    case CTC_ACL_FIELD_ACTION_DOT1AE_SPI:
    {
        CTC_MAX_VALUE_CHECK(action_field->data0, MCHIP_CAP(SYS_CAP_DOT1AE_SPI_MAX));
        SetDsEpeAcl(V, gXSec_ctcSpi_f, p_buffer->action, is_add? action_field->data0: 0);
        break;
    }

    case CTC_ACL_FIELD_ACTION_OAM:
        SYS_CHECK_UNION_BITMAP(pe->u2_type,SYS_AD_UNION_G_2);
        if(is_add)
        {
            CTC_PTR_VALID_CHECK(action_field->ext_data);
            p_oam = (ctc_acl_oam_t*)(action_field->ext_data);
            CTC_MAX_VALUE_CHECK(p_oam ->timestamp_format, 1);
            SetDsEpeAcl(V, u2_gOam_oamDestChip_f, p_buffer->action, p_oam ->dest_gchip);
            SetDsEpeAcl(V, u2_gOam_oamDestSliceId_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u2_gOam_mepIndex_f, p_buffer->action, p_oam ->lmep_index);
            SetDsEpeAcl(V, u2_gOam_mipEn_f, p_buffer->action, p_oam ->mip_en? 1 : 0);
            SetDsEpeAcl(V, u2_gOam_rxOamType_f, p_buffer->action, p_oam ->oam_type);
            SetDsEpeAcl(V, u2_gOam_timestampEn_f, p_buffer->action, p_oam ->time_stamp_en? 1 : 0);
        }
        else
        {
            SetDsEpeAcl(V, u2_gOam_oamDestChip_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u2_gOam_oamDestSliceId_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u2_gOam_mepIndex_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u2_gOam_mipEn_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u2_gOam_rxOamType_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u2_gOam_timestampEn_f, p_buffer->action, 0);
        }
        SYS_SET_UNION_TYPE(pe->u2_type,SYS_AD_UNION_G_2,DsEpeAcl,2,p_buffer->action, is_add);
        temp_u2_type = DRV_FROM_AT(lchip) ? 2: 1;
        SetDsEpeAcl(V, u2Type_f, p_buffer->action, (pe->u2_type == SYS_AD_UNION_G_2? temp_u2_type: 0));
        break;
    case CTC_ACL_FIELD_ACTION_TIMESTAMP:
        {
            ctc_acl_timestamp_t* timestamp = (ctc_acl_timestamp_t*)action_field->ext_data;

            if (is_add)
            {
                CTC_PTR_VALID_CHECK(action_field->ext_data);
                CTC_MAX_VALUE_CHECK(timestamp->mode, 1);
            }
            SetDsEpeAcl(V, packetTsMode_f, p_buffer->action, is_add? timestamp->mode : 0);
        }
        break;

    case CTC_ACL_FIELD_ACTION_CANCEL_ALL:

        reason_info.reason_id = p_buffer->ad.tcam.cpu_reason_id;
        sys_usw_cpu_reason_free_exception_index(lchip, pe->group->group_info.dir, &reason_info);
        p_buffer->ad.tcam.cpu_reason_id = 0;

        if (0 != p_buffer->ad.tcam.stats_id)
        {
            sys_stats_param_t sts_param;
            sal_memset(&sts_param, 0, sizeof(sts_param));
            sts_param.id = p_buffer->ad.tcam.stats_id;
            sts_param.type = SYS_STATS_TYPE_ACL;
            sts_param.dir = pe->group->group_info.dir;
            sys_usw_flow_stats_free_statsptr(lchip, &sts_param);
            p_buffer->ad.tcam.stats_id = 0;
        }

        if(SYS_ACL_ACTION_POLICER_MODE_MICRO_FLOW == pe->policer_mode)
        {
            uint16 policer_ptr = GetDsEpeAcl(V, gPolicer_policerPtr_f, p_buffer->action);

            sal_memset(&policer_group_info, 0, sizeof(sys_qos_policer_group_info_t));
            policer_group_info.dir = pe->group->group_info.dir;
            policer_group_info.policer_member_ptr = policer_ptr;
            sys_usw_qos_policer_group_unbind(lchip, &policer_group_info);
        }
        sal_memset(p_buffer->action, 0, sizeof(p_buffer->action));

        pe->u1_type = 0;
        pe->u2_type = 0;
        pe->u3_type = 0;
        pe->u4_type = 0;
        break;

    case CTC_ACL_FIELD_ACTION_CANCEL_DISCARD:
    case CTC_ACL_FIELD_ACTION_DISCARD:
        CTC_MAX_VALUE_CHECK(action_field->data0, 14);
        if (action_field->data0&0x1)
        {
            return CTC_E_INVALID_PARAM;
        }
        data1 = (action_field->type == CTC_ACL_FIELD_ACTION_CANCEL_DISCARD)?2:1;

        if (CTC_IS_BIT_SET(action_field->data0, CTC_QOS_COLOR_RED) || CTC_QOS_COLOR_NONE == action_field->data0)
        {
            if(is_add || data1 == GetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action))
            {
                SetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action, is_add?data1:0);
            }
        }
        if (CTC_IS_BIT_SET(action_field->data0, CTC_QOS_COLOR_YELLOW) || CTC_QOS_COLOR_NONE == action_field->data0)
        {
            if(is_add || data1 == GetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action))
            {
                SetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action, is_add?data1:0);
            }
        }
        if (CTC_IS_BIT_SET(action_field->data0, CTC_QOS_COLOR_GREEN) || CTC_QOS_COLOR_NONE == action_field->data0)
        {
            if(is_add || data1 == GetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action))
            {
                SetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action, is_add?data1:0);
            }
        }

        if(1 == GetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action) && 1 == GetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action) && 1 == GetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action))
        {
            //p_buffer->ad.tcam.action_flag |= SYS_ACL_ACTION_FLAG_DISCARD;
            //p_buffer->ad.tcam.action_flag &= (~SYS_ACL_ACTION_FLAG_COLOR_BASED);
        }
        else if (1 == GetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action) || 1 == GetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action) || 1 == GetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action))
        {
            //p_buffer->ad.tcam.action_flag |= SYS_ACL_ACTION_FLAG_COLOR_BASED;
            //p_buffer->ad.tcam.action_flag &= (~SYS_ACL_ACTION_FLAG_DISCARD);
        }
        else
        {
            //p_buffer->ad.tcam.action_flag &= (~SYS_ACL_ACTION_FLAG_COLOR_BASED);
            //p_buffer->ad.tcam.action_flag &= (~SYS_ACL_ACTION_FLAG_DISCARD);
        }
        break;

    case CTC_ACL_FIELD_ACTION_CP_TO_CPU:

        if (is_add)
        {
            uint16 old_cpu_reason_id = p_buffer->ad.tcam.cpu_reason_id;
            CTC_PTR_VALID_CHECK(action_field->ext_data);
            p_cp_to_cpu = (ctc_acl_to_cpu_t*)action_field->ext_data;
            CTC_MAX_VALUE_CHECK(p_cp_to_cpu->mode, CTC_ACL_TO_CPU_MODE_MAX - 1);
            if (p_cp_to_cpu->mode == CTC_ACL_TO_CPU_MODE_CANCEL_TO_CPU)
            {
                SetDsEpeAcl(V, gExcep_exceptionToCpuType_f, p_buffer->action, 2);
                if (old_cpu_reason_id)
                {
                    reason_info.reason_id = p_buffer->ad.tcam.cpu_reason_id;
                    CTC_ERROR_RETURN(sys_usw_cpu_reason_free_exception_index(lchip, pe->group->group_info.dir, &reason_info));
                }
                p_buffer->ad.tcam.cpu_reason_id = 0;
            }
            else
            {
                p_cp_to_cpu->cpu_reason_id = (p_cp_to_cpu->mode == CTC_ACL_TO_CPU_MODE_TO_CPU_NOT_COVER)?
                CTC_PKT_CPU_REASON_ACL_MATCH : p_cp_to_cpu->cpu_reason_id;
                if (old_cpu_reason_id && (old_cpu_reason_id == p_cp_to_cpu->cpu_reason_id))
                {
                    return CTC_E_NONE;
                }
                reason_info.reason_id = p_cp_to_cpu->cpu_reason_id;
                CTC_ERROR_RETURN(sys_usw_cpu_reason_alloc_exception_index(lchip, pe->group->group_info.dir, &reason_info));
                SetDsEpeAcl(V, gExcep_exceptionIndex_f, p_buffer->action, reason_info.exception_index);
                SetDsEpeAcl(V, gExcep_exceptionSubIndex_f, p_buffer->action, reason_info.exception_subIndex);
                data0 = (p_cp_to_cpu->mode == CTC_ACL_TO_CPU_MODE_TO_CPU_NOT_COVER) ? 3:1;
                SetDsEpeAcl(V, gExcep_exceptionToCpuType_f, p_buffer->action, data0);
                p_buffer->ad.tcam.cpu_reason_id = (p_cp_to_cpu->mode == CTC_ACL_TO_CPU_MODE_TO_CPU_NOT_COVER)? 0 : p_cp_to_cpu->cpu_reason_id;
                if (old_cpu_reason_id)
                {
                    reason_info.reason_id = old_cpu_reason_id;
                    CTC_ERROR_RETURN(sys_usw_cpu_reason_free_exception_index(lchip, pe->group->group_info.dir, &reason_info));
                }
            }
        }
        else
        {
            if (p_buffer->ad.tcam.cpu_reason_id)
            {
                reason_info.reason_id = p_buffer->ad.tcam.cpu_reason_id;
                sys_usw_cpu_reason_free_exception_index(lchip, pe->group->group_info.dir, &reason_info);
            }
            SetDsEpeAcl(V, gExcep_exceptionToCpuType_f, p_buffer->action, 0);
            p_buffer->ad.tcam.cpu_reason_id = 0;
        }
        break;

    case CTC_ACL_FIELD_ACTION_CANCEL_IPFIX:
        if (DRV_FROM_AT(lchip))
        {
            SYS_USW_ACL_IPFIX_CONFLICT_CHECK(2, p_buffer);
            SetDsEpeAcl(V, ipfixOpCode_f, p_buffer->action, is_add?2:0);
        }
        else
        {
            SetDsEpeAcl(V, disableIpfix_f, p_buffer->action, is_add?1:0);
        }
        break;
    case CTC_ACL_FIELD_ACTION_MICRO_FLOW_POLICER:
        SYS_USW_ACL_CHECK_ACTION_POLICER_MODE(pe, SYS_ACL_ACTION_POLICER_MODE_MICRO_FLOW);
        if (is_add)
        {
            is_glb_entry = (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, pe->group->group_info.dir, pe->group->group_info.block_id)) ? 0 : 1;
            /* two level policer check for ARCTIC */
            if(DRV_FROM_AT(lchip) && data1 && is_glb_entry)
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Do not support this two level policer for global group\n");
                return CTC_E_NOT_SUPPORT;
            }

            CTC_NOT_EQUAL_CHECK(data0, 0);
            CTC_NOT_EQUAL_CHECK(data0, data1);

            if (SYS_ACL_ACTION_POLICER_MODE_NONE == pe->policer_mode && DRV_FROM_TMM(lchip))
            {
                sal_memset(&policer_group_info, 0, sizeof(sys_qos_policer_group_info_t));
                policer_group_info.dir = pe->group->group_info.dir;
                policer_group_info.policer_id = data0;
                CTC_ERROR_RETURN(sys_usw_qos_policer_group_check_bind(lchip, &policer_group_info));
                CTC_ERROR_RETURN(policer_group_info.is_bind? CTC_E_IN_USE: CTC_E_NONE);
            }
            policer_param.policer_id = data0;
            policer_param.dir = pe->group->group_info.dir;
            policer_param.sys_policer_type = SYS_QOS_POLICER_TYPE_FLOW;
            policer_param.glb_policer = is_glb_entry;
            CTC_ERROR_RETURN(sys_usw_qos_policer_index_get(lchip, &policer_param));

            if ((policer_param.policer_ptr > 0x1FFF) || (policer_param.policer_ptr == 0))
            {
                return CTC_E_INVALID_PARAM;
            }
            sal_memset(&policer_group_info, 0, sizeof(sys_qos_policer_group_info_t));
            if(data1)
            {
                policer_grp_param.policer_id = data1;
                policer_grp_param.dir = pe->group->group_info.dir;
                policer_grp_param.sys_policer_type = SYS_QOS_POLICER_TYPE_GROUP_FLOW;
                CTC_ERROR_RETURN(sys_usw_qos_policer_index_get(lchip, &policer_grp_param));

                if (policer_grp_param.policer_ptr == 0)
                {
                    return CTC_E_INVALID_PARAM;
                }
                policer_group_info.dir = pe->group->group_info.dir;
                policer_group_info.policer_group_ptr = policer_grp_param.policer_ptr;
                policer_group_info.policer_member_ptr = policer_param.policer_ptr;
                CTC_ERROR_RETURN(sys_usw_qos_policer_group_bind(lchip, &policer_group_info));
            }
            else
            {
                policer_group_info.dir = pe->group->group_info.dir;
                policer_group_info.policer_member_ptr = policer_param.policer_ptr;
                CTC_ERROR_RETURN(sys_usw_qos_policer_group_unbind(lchip, &policer_group_info));
            }
            //p_buffer->ad.tcam.policer_id = data0;
        }
        else
        {
            uint16 policer_ptr = GetDsEpeAcl(V, gPolicer_policerPtr_f, p_buffer->action);
            
            sal_memset(&policer_group_info, 0, sizeof(sys_qos_policer_group_info_t));
            policer_group_info.dir = pe->group->group_info.dir;
            policer_group_info.policer_member_ptr = policer_ptr;
            CTC_ERROR_RETURN(sys_usw_qos_policer_group_unbind(lchip, &policer_group_info));

        }
        SetDsEpeAcl(V, gPolicer_policerLvlSel_f, p_buffer->action, is_add ? policer_param.level : 0);
        SetDsEpeAcl(V, gPolicer_policerPtr_f, p_buffer->action, is_add ?policer_param.policer_ptr:0);
        SetDsEpeAcl(V, gPolicer_policerPhbEn_f, p_buffer->action, is_add?policer_param.is_bwp:0);
        pe->policer_mode = is_add?SYS_ACL_ACTION_POLICER_MODE_MICRO_FLOW : 0;
        break;

    case CTC_ACL_FIELD_ACTION_MACRO_FLOW_POLICER:
        SYS_USW_ACL_CHECK_ACTION_POLICER_MODE(pe, SYS_ACL_ACTION_POLICER_MODE_MACRO_FLOW);
        if (is_add)
        {
            CTC_NOT_EQUAL_CHECK(data0, 0);
            policer_param.policer_id = data0;
            policer_param.dir = pe->group->group_info.dir;
            policer_param.sys_policer_type = SYS_QOS_POLICER_TYPE_MACRO_FLOW;
            policer_param.glb_policer = (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, pe->group->group_info.dir, pe->group->group_info.block_id)) ? 0 : 1;
            CTC_ERROR_RETURN(sys_usw_qos_policer_index_get(lchip, &policer_param));

            if ((policer_param.policer_ptr > 0x1FFF) || (policer_param.policer_ptr == 0))
            {
                return CTC_E_INVALID_PARAM;
            }
        }
        SetDsEpeAcl(V, gPolicer_policerLvlSel_f, p_buffer->action, is_add ? policer_param.level : 0);
        SetDsEpeAcl(V, gPolicer_policerPtr_f, p_buffer->action, is_add ?policer_param.policer_ptr:0);
        SetDsEpeAcl(V, gPolicer_policerPhbEn_f, p_buffer->action, is_add?policer_param.is_bwp:0);
        pe->policer_mode=is_add?SYS_ACL_ACTION_POLICER_MODE_MACRO_FLOW:0;
        break;

    case CTC_ACL_FIELD_ACTION_COS_HBWP_POLICER:
        SYS_USW_ACL_CHECK_ACTION_POLICER_MODE(pe, SYS_ACL_ACTION_POLICER_MODE_COS_HBWP);
        if (is_add)
        {
            CTC_NOT_EQUAL_CHECK(data0, 0);
            CTC_MAX_VALUE_CHECK(data1, MCHIP_CAP(SYS_CAP_QOS_POLICER_MAX_COS_LEVEL) -1);

            policer_param.policer_id = data0;
            policer_param.dir = pe->group->group_info.dir;
            policer_param.sys_policer_type = SYS_QOS_POLICER_TYPE_MACRO_FLOW;
            policer_param.glb_policer = (MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL) && !SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, pe->group->group_info.dir, pe->group->group_info.block_id)) ? 0 : 1;
            CTC_ERROR_RETURN(sys_usw_qos_policer_index_get(lchip, &policer_param));

            if (!policer_param.is_bwp)
            {
                return CTC_E_INVALID_PARAM;
            }
        }
        SetDsEpeAcl(V, gPolicer_policerLvlSel_f, p_buffer->action, is_add ? policer_param.level : 0);
        SetDsEpeAcl(V, gPolicer_policerPtr_f, p_buffer->action, is_add ?(policer_param.policer_ptr + data1):0);
        SetDsEpeAcl(V, gPolicer_policerPhbEn_f, p_buffer->action, 0);
        pe->policer_mode=is_add?SYS_ACL_ACTION_POLICER_MODE_COS_HBWP:0;
        pe->cos_index = is_add ? data1 : 0;
        break;

    case CTC_ACL_FIELD_ACTION_COPP:
        SYS_USW_ACL_CHECK_ACTION_POLICER_MODE(pe, SYS_ACL_ACTION_POLICER_MODE_COPP);

        if (is_add)
        {
            CTC_NOT_EQUAL_CHECK(data0, 0);
            policer_param.policer_id = data0;
            policer_param.dir = CTC_EGRESS;
            policer_param.glb_policer = 1;
            policer_param.sys_policer_type = SYS_QOS_POLICER_TYPE_COPP;
            CTC_ERROR_RETURN(sys_usw_qos_policer_index_get(lchip, &policer_param));

            if ((policer_param.policer_ptr > 0x1FFF) || (policer_param.policer_ptr == 0))
            {
                return CTC_E_INVALID_PARAM;
            }

            if (policer_param.is_bwp)
            {
                return CTC_E_INVALID_PARAM;
            }
        }

        SetDsEpeAcl(V, gPolicer_coppValid_f, p_buffer->action, is_add ? 1:0);
        SetDsEpeAcl(V, gPolicer_policerLvlSel_f, p_buffer->action, 0);
        SetDsEpeAcl(V, gPolicer_policerPtr_f, p_buffer->action, is_add ? policer_param.policer_ptr : 0);
        pe->policer_mode=is_add?SYS_ACL_ACTION_POLICER_MODE_COPP:0;
        break;

    case CTC_ACL_FIELD_ACTION_TRUNCATED_LEN:
        if (is_add)
        {
            CTC_ERROR_RETURN(sys_usw_register_get_truncation_profile_id(lchip, data0, sizeof(data0)*CTC_UINT8_BITS, &profile_id));
            SetDsEpeAcl(V, truncateLenProfId_f, p_buffer->action, profile_id);
        }
        else
        {
            SetDsEpeAcl(V, truncateLenProfId_f, p_buffer->action, 0);
        }

        break;

    case CTC_ACL_FIELD_ACTION_STATS:
    {
        sys_stats_param_t stats;
        sal_memset(&stats, 0, sizeof(stats));
        if (!im_flow_valid)
        {
            stats.dir = pe->group->group_info.dir;
            stats.type = SYS_STATS_TYPE_ACL;
            
            if (is_add)
            {
                uint8  block_id = pe->group->group_info.block_id+ACL_IGS_BLOCK_MAX_NUM;

                if(DRV_FROM_AT(lchip))
                {
                    uint8 key_size = _sys_usw_acl_get_key_size(lchip, 1, pe, NULL);
                    sys_acl_block_t* pb_tmp = &SYS_ACL_BLOCK(lchip, block_id);
                    ACL_MAP_FLOW_STATS_BLOCK_ID(pe, pb_tmp, block_id, key_size);
                }
                else
                {
                    ACL_MAP_FLOW_STATS_BLOCK_ID0(pe, block_id);
                }
                if(data0==p_buffer->ad.tcam.stats_id)
                {
                    /*only update care discard*/
                    SetDsEpeAcl(V, statsCareDiscard_f, p_buffer->action, (!data1));
                    break;
                }
                stats.id = data0;
                stats.acl_pri = block_id;
                CTC_ERROR_RETURN(sys_usw_flow_stats_alloc_statsptr(lchip, &stats));
                stats_ptr = stats.ptr;
                stats_pool_num = stats.ram_id;
            }
            /*update and remove, all should to free acl statsptr*/
            if (p_buffer->ad.tcam.stats_id)
            {
                sal_memset(&stats, 0, sizeof(stats));
                stats.id = p_buffer->ad.tcam.stats_id;
                stats.dir = pe->group->group_info.dir;
                sys_usw_flow_stats_free_statsptr(lchip, &stats);
            }
            p_buffer->ad.tcam.stats_id = data0;
        }
        else
        {
            stats_ptr = data0;
        }

        SetDsEpeAcl(V, statsPtr_f, p_buffer->action, stats_ptr);
        SetDsEpeAcl(V, statsValid_f, p_buffer->action, is_add ? 1 : 0);
        SetDsEpeAcl(V, statsCareDiscard_f, p_buffer->action, is_add ? (!data1) : 0);
        SetDsEpeAcl(V, statsColorAware_f, p_buffer->action, (stats.num==3));
        SetDsEpeAcl(V, statsPoolNumber_f, p_buffer->action, stats_pool_num);
        break;
    }
    case CTC_ACL_FIELD_ACTION_VLAN_EDIT:

        if (is_add)
        {
            CTC_PTR_VALID_CHECK(action_field->ext_data);
            p_ctc_vlan_edit = (ctc_acl_vlan_edit_t*)(action_field->ext_data);
            CTC_ERROR_RETURN(_sys_usw_acl_check_vlan_edit(lchip, p_ctc_vlan_edit, &do_vlan_edit));

            /*Write vlan edit*/
            if (do_vlan_edit)
            {
                vlan_edit.stag_op = p_ctc_vlan_edit->stag_op;
                vlan_edit.ctag_op = p_ctc_vlan_edit->ctag_op;
                vlan_edit.svid_sl = p_ctc_vlan_edit->svid_sl;
                vlan_edit.scos_sl = p_ctc_vlan_edit->scos_sl;
                vlan_edit.scfi_sl = p_ctc_vlan_edit->scfi_sl;
                vlan_edit.cvid_sl = p_ctc_vlan_edit->cvid_sl;
                vlan_edit.ccos_sl = p_ctc_vlan_edit->ccos_sl;
                vlan_edit.ccfi_sl = p_ctc_vlan_edit->ccfi_sl;
            }
            if (CTC_ACL_VLAN_TAG_OP_NONE != vlan_edit.ctag_op)
            {
                SYS_CHECK_UNION_BITMAP(pe->u2_type, SYS_AD_UNION_G_1);
            }
            if(CTC_ACL_VLAN_TAG_OP_NONE != vlan_edit.stag_op)
            {
                SYS_CHECK_UNION_BITMAP(pe->u1_type, SYS_AD_UNION_G_1);
            }
        }

        if ((CTC_ACL_VLAN_TAG_OP_NONE != vlan_edit.stag_op) || SYS_AD_UNION_G_1 == pe->u1_type)
        {
            uint8 op = 0;
            uint8 mo = 0;
            if(!is_add)
            {
                if(pe->u1_type != SYS_AD_UNION_G_NA && pe->u1_type != SYS_AD_UNION_G_1 )
                {
                    return CTC_E_NONE;
                }
            }
            _sys_usw_acl_vlan_tag_op_translate(lchip, vlan_edit.stag_op, &op, &mo);
            SetDsEpeAcl(V, gSvlanAction_sTagAction_f, p_buffer->action, is_add? op: 0);
            SetDsEpeAcl(V, gSvlanAction_stagModifyMode_f, p_buffer->action, is_add? mo: 0);
            SetDsEpeAcl(V, gSvlanAction_sVlanIdAction_f, p_buffer->action, is_add? p_ctc_vlan_edit->svid_sl: 0);
            SetDsEpeAcl(V, gSvlanAction_sCosAction_f, p_buffer->action, is_add? p_ctc_vlan_edit->scos_sl: 0);
            SetDsEpeAcl(V, gSvlanAction_sCfiAction_f, p_buffer->action, is_add? p_ctc_vlan_edit->scfi_sl: 0);
            SetDsEpeAcl(V, gSvlanAction_svlanTpidIndexEn_f, p_buffer->action, (is_add && 0xff != p_ctc_vlan_edit->tpid_index)? 1: 0);
            SetDsEpeAcl(V, gSvlanAction_svlanTpidIndex_f, p_buffer->action, is_add? p_ctc_vlan_edit->tpid_index: 0);
            SetDsEpeAcl(V, gSvlanAction_svlanId_f, p_buffer->action, is_add? p_ctc_vlan_edit->svid_new: 0);
            SetDsEpeAcl(V, gSvlanAction_scos_f, p_buffer->action, is_add? p_ctc_vlan_edit->scos_new: 0);
            SetDsEpeAcl(V, gSvlanAction_scfi_f, p_buffer->action, is_add? p_ctc_vlan_edit->scfi_new: 0);

            pe->u1_type = is_add ? SYS_AD_UNION_G_1 : 0;
            SetDsEpeAcl(V, u1Type_f, p_buffer->action, 0);
        }
        if ((CTC_ACL_VLAN_TAG_OP_NONE != vlan_edit.ctag_op) || SYS_AD_UNION_G_1 == pe->u2_type)
        {
            uint8 op = 0;
            uint8 mo = 0;
            if(!is_add)
            {
                if(pe->u2_type != SYS_AD_UNION_G_NA && pe->u2_type != SYS_AD_UNION_G_1 )
                {
                    return CTC_E_NONE;
                }
            }
            _sys_usw_acl_vlan_tag_op_translate(lchip, vlan_edit.ctag_op, &op, &mo);
            SetDsEpeAcl(V, u2_gCvlanAction_cTagAction_f, p_buffer->action, is_add? op: 0);
            SetDsEpeAcl(V, u2_gCvlanAction_ctagModifyMode_f, p_buffer->action, is_add? mo: 0);
            SetDsEpeAcl(V, u2_gCvlanAction_cVlanIdAction_f, p_buffer->action, is_add? p_ctc_vlan_edit->cvid_sl: 0);
            SetDsEpeAcl(V, u2_gCvlanAction_cCosAction_f, p_buffer->action, is_add? p_ctc_vlan_edit->ccos_sl: 0);
            SetDsEpeAcl(V, u2_gCvlanAction_cCfiAction_f, p_buffer->action, is_add? p_ctc_vlan_edit->ccfi_sl: 0);
            SetDsEpeAcl(V, u2_gCvlanAction_ctagAddMode_f, p_buffer->action, 0);
            SetDsEpeAcl(V, u2_gCvlanAction_cvlanId_f, p_buffer->action, is_add? p_ctc_vlan_edit->cvid_new: 0);
            SetDsEpeAcl(V, u2_gCvlanAction_ccos_f, p_buffer->action, is_add? p_ctc_vlan_edit->ccos_new: 0);
            SetDsEpeAcl(V, u2_gCvlanAction_ccfi_f, p_buffer->action, is_add? p_ctc_vlan_edit->ccfi_new: 0);

            pe->u2_type = is_add ? SYS_AD_UNION_G_1 : 0;
            temp_u2_type = DRV_FROM_AT(lchip) && is_add ? 2 : 0;
            SetDsEpeAcl(V, u2Type_f, p_buffer->action, temp_u2_type);
        }
        break;      /*egress vlan edit end*/

    case CTC_ACL_FIELD_ACTION_RANDOM_LOG:
    {
        uint8  log_id = 0;
        uint8  session_id = 0;
        uint8  int_en = 0;
        ctc_log_percent_t percent = 0;
        ctc_acl_log_t* p_log = NULL;

        CTC_ERROR_RETURN(sys_usw_global_get_int_en(lchip, &int_en));
        if (is_add)
        {
            if (action_field->ext_data)
            {
                p_log = (ctc_acl_log_t*)(action_field->ext_data);
                log_id = p_log->log_id;
                session_id = p_log->session_id;
                percent = p_log->percent;
            }
            else
            {
                log_id = pe->group->group_info.block_id;
                session_id = data0;
                percent = data1;
            }
            CTC_MAX_VALUE_CHECK(log_id, MCHIP_CAP(SYS_CAP_ACL_EGS_MAX_LOG_ID)-(!!int_en));
            CTC_MAX_VALUE_CHECK(session_id, MCHIP_CAP(SYS_CAP_ACL_MAX_SESSION) - 1);
            CTC_VALUE_RANGE_CHECK(percent, CTC_LOG_PERCENT_POWER_NEGATIVE_14, CTC_LOG_PERCENT_POWER_NEGATIVE_0);
        }
        SetDsEpeAcl(V, gLog_randomThresholdShift_f, p_buffer->action, percent);
        SetDsEpeAcl(V, gLog_aclLogId_f, p_buffer->action, (log_id<<2)|session_id);
        //p_buffer->ad.tcam.action_flag = is_add ? (p_buffer->ad.tcam.action_flag | SYS_ACL_ACTION_FLAG_LOG) : (p_buffer->ad.tcam.action_flag & (~SYS_ACL_ACTION_FLAG_LOG));
        break;
    }

    case CTC_ACL_FIELD_ACTION_CANCEL_DOT1AE:
        if(DRV_FROM_AT(lchip))
        {
            SetDsEpeAcl(V, gXSec_bypassMacsecProc_f, p_buffer->action, is_add? 1: 0);
        }
        else
        {
            SetDsEpeAcl(V, gMacSec_bypassMacsecProc_f, p_buffer->action, is_add? 1: 0);
        }

        break;
	case CTC_ACL_FIELD_ACTION_ECN:
	    SetDsEpeAcl(V, gMacSec_isAdvancedMacsec_f , p_buffer->action, is_add?1: 0);
		SetDsEpeAcl(V, gMacSec_transmitScId_f , p_buffer->action, is_add?data0: 0);
		SetDsEpeAcl(V, gMacSec_secTagOffsetType_f , p_buffer->action, (is_add&&(data0>>6))?1: 0);
		break;
    default:
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, " Feature not supported \n");
        return CTC_E_NOT_SUPPORT;
    }

    if (1 == im_flow_valid)
    {
        action_field->type = CTC_ACL_FIELD_ACTION_NPM_IM_FLOW_ID;
        action_field->data0 = im_flow_id;
        action_field->data1 = 0;
    }

    /*update action_bmp*/
    /*if(SYS_ACL_ASET_LEVEL_ACTION == pe->aset_level)
    {
        if (im_flow_valid)
        {
            SYS_USW_ACL_SET_BMP(pe->aset->w, CTC_ACL_FIELD_ACTION_NPM_IM_FLOW_ID, is_add);
            SYS_USW_ACL_SET_BMP(pe->aset->w, CTC_ACL_FIELD_ACTION_STATS, !is_add);
        }

        if ((CTC_ACL_FIELD_ACTION_DISCARD != action_field->type) && (CTC_ACL_FIELD_ACTION_CANCEL_DISCARD != action_field->type))
        {
            SYS_USW_ACL_SET_BMP(pe->aset->w, action_field->type, is_add);
        }
        else
        {
            if (is_add)
            {
                SYS_USW_ACL_SET_BMP(pe->aset->w, action_field->type, is_add);
            }
            else
            {
                if( (data1 != GetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action)) && (data1 != GetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action)) && (data1 != GetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action)))
                {
                    CTC_BMP_UNSET(pe->aset->w, action_field->type);
                }
            }
        }

        if (CTC_ACL_FIELD_ACTION_CANCEL_ALL == action_field->type)
        {
            sal_memset(pe->aset->w, 0, CTC_B2W_SIZE(CTC_ACL_FIELD_ACTION_NUM)* sizeof(uint32));
        }
    }*/

    return CTC_E_NONE;
}

int32
sys_tmm_acl_get_egs_action_field(uint8 lchip, ctc_acl_field_action_t* action_field, void* pe_info)
{
    uint32  data1        = 0;
    uint32 cmd = 0;
    ctc_acl_vlan_edit_t*    p_ctc_vlan_edit = NULL;
    sys_acl_buffer_t*       p_buffer = NULL;
    ctc_acl_to_cpu_t*       p_cp_to_cpu = NULL;
    ctc_acl_oam_t*          p_oam = NULL;
    ctc_acl_timestamp_t* timestamp = NULL;
    ctc_acl_log_t *p_log = NULL;
    sys_qos_policer_param_t policer_param,policer_grp_param;
    sys_qos_policer_group_info_t policer_group_info;
    sys_cpu_reason_info_t   reason_info;
    sys_acl_entry_t* pe  = NULL;
    uint8  shift = 0;
    uint8  op = 0, mo = 0;
    uint8  log_id = 0;
    uint8  percent = 0;
    uint16 length = 0;
    DsTruncationProfile_m trunc_prof;


    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    pe = (sys_acl_entry_t *) pe_info;

    p_buffer = &pe->ua->buffer;
    sal_memset(&policer_param, 0, sizeof(sys_qos_policer_param_t));
    sal_memset(&policer_grp_param, 0, sizeof(sys_qos_policer_param_t));
    sal_memset(&policer_group_info, 0, sizeof(sys_qos_policer_group_info_t));
    sal_memset(&reason_info, 0, sizeof(sys_cpu_reason_info_t));

    switch(action_field->type)
    {
    case CTC_ACL_FIELD_ACTION_XDATA:
    {
        uint32 xdata[3] = {0};
        ctc_xdata_t* p_xdata = NULL;
        CTC_PTR_VALID_CHECK(action_field->ext_data);
        CTC_ERROR_RETURN(DRV_FROM_AT(lchip)? CTC_E_NONE: CTC_E_NOT_SUPPORT);

        p_xdata = action_field->ext_data;
        p_xdata->prof_id = GetDsEpeAcl(V, xAdIndex_f, p_buffer->action);
        GetDsEpeAcl(A, xAdData_f, p_buffer->action, xdata);
        if(GetDsEpeAcl(V, xAdEn_f, p_buffer->action) && CTC_E_NONE == MCHIP_XDATA(lchip)->prof_get_info(lchip, p_xdata , FALSE))
        {
            uint8 loop = 0;
            ctc_xdata_field_t* p_field = NULL;
            action_field->data1 = 1;
            for(loop = 0; loop<p_xdata->num; loop++)
            {
                p_field = p_xdata->fields+loop;
                SYS_USW_GET_XDATA(p_field->width, p_field->offset, p_field->data, xdata);
            }
        }
    }
        break;
    case CTC_ACL_FIELD_ACTION_IPFIX:
        {
            ctc_acl_ipfix_t* p_ipfix = action_field->ext_data;
            CTC_PTR_VALID_CHECK(p_ipfix);
            if(1 == GetDsEpeAcl(V, u2Type_f, p_buffer->action))
            {
                p_ipfix->flow_cfg_id = GetDsEpeAcl(V, u2_gIpfix_ipfixCfgProfileId_f, p_buffer->action);
                p_ipfix->field_sel_id = GetDsEpeAcl(V, u2_gIpfix_ipfixHashFieldSel_f, p_buffer->action);
                p_ipfix->hash_type = GetDsEpeAcl(V, u2_gIpfix_ipfixHashType_f, p_buffer->action);
                p_ipfix->use_mapped_vlan = GetDsEpeAcl(V, u2_gIpfix_ipfixUsePIVlan_f, p_buffer->action);
                p_ipfix->policer_id= 0;
                p_ipfix->lantency_en = GetDsEpeAcl(V, u2_gIpfix_egrIpfixAdUseMeasureLantency_f, p_buffer->action);
                p_ipfix->flex_key_en = GetDsEpeAcl(V, u2_gIpfix_ipfixXKeyEn_f, p_buffer->action);
                p_ipfix->cancel_learning = GetDsEpeAcl(V, u2_gIpfix_denyIpfixInsertOperation_f, p_buffer->action);
                action_field->data1 = GetDsEpeAcl(V, ipfixOpCode_f, p_buffer->action) == 1;
            }
        }
        break;
    case CTC_ACL_FIELD_ACTION_IPFIX_HASH_SALT:
        if( 1== GetDsEpeAcl(V, u1Type_f, p_buffer->action))
        {
            action_field->data0 = GetDsEpeAcl(V, u1_gOam_xAdData_f, p_buffer->action);
            action_field->data1 = GetDsEpeAcl(V, u1_gOam_ipfixHashKeySaltEn_f, p_buffer->action);
        }
        break;
    case CTC_ACL_FIELD_ACTION_CANCEL_IPFIX_LEARNING:
        action_field->data0 = GetDsEpeAcl(V, ipfixOpCode_f, p_buffer->action) == 3? 1: 0;
        break;
    case CTC_ACL_FIELD_ACTION_NPM_IM_FLOW_ID:
        action_field->data0 = 0;
        action_field->data1 = 0;
        break;
    case CTC_ACL_FIELD_ACTION_DOT1AE_CHAN_ID:
        if (DRV_FROM_TMG(lchip))
        {
            return CTC_E_NOT_SUPPORT;
        }
        //action_field->data0 = p_buffer->ad.tcam.dot1ae_chan_id;
        action_field->data0 = GetDsEpeAcl(V, gMacSec_transmitScId_f, p_buffer->action);
        action_field->data1 = GetDsEpeAcl(V, gMacSec_secTagOffsetType_f, p_buffer->action);
        break;
    case CTC_ACL_FIELD_ACTION_DOT1AE_SPI:
        action_field->data0 = GetDsEpeAcl(V, gXSec_ctcSpi_f, p_buffer->action);
        break;
    case CTC_ACL_FIELD_ACTION_OAM:
        if(pe->u2_type == SYS_AD_UNION_G_2)
        {
            uint32 value[4] = {0};
            CTC_PTR_VALID_CHECK(action_field->ext_data);
            p_oam = (ctc_acl_oam_t*)(action_field->ext_data);
            p_oam ->dest_gchip = GetDsEpeAcl(V, u2_gOam_oamDestChip_f, p_buffer->action);
            p_oam ->lmep_index = GetDsEpeAcl(V, u2_gOam_mepIndex_f, p_buffer->action);
            p_oam ->mip_en = GetDsEpeAcl(V, u2_gOam_mipEn_f, p_buffer->action);
            p_oam ->oam_type = GetDsEpeAcl(V, u2_gOam_rxOamType_f, p_buffer->action);
            p_oam ->time_stamp_en = GetDsEpeAcl(V, u2_gOam_timestampEn_f, p_buffer->action);
            GetDsEpeAcl(A, u2_data_f, p_buffer->action, value);
            if(value[0] || value[1] || value[2] || value[3])
            {
                action_field->data1 = 1;
            }
        }
        break;
    case CTC_ACL_FIELD_ACTION_TIMESTAMP:
        {
            CTC_PTR_VALID_CHECK(action_field->ext_data);
            timestamp = (ctc_acl_timestamp_t*)action_field->ext_data;
            timestamp->mode = GetDsEpeAcl(V, packetTsMode_f, p_buffer->action);
            action_field->data1 = timestamp->mode ? 1 : 0;
        }
        break;

    case CTC_ACL_FIELD_ACTION_CANCEL_ALL:
        break;

    case CTC_ACL_FIELD_ACTION_CANCEL_DISCARD:
        if(action_field->data0 == CTC_QOS_COLOR_NONE)
        {
             action_field->data1 = (GetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action) == 2) \
                                 || (GetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action) == 2) \
                                 || (GetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action) == 2);
        }
        else if (action_field->data0 == CTC_QOS_COLOR_GREEN){
            action_field->data1 = GetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action) == 2;
        }
        else if (action_field->data0 == CTC_QOS_COLOR_YELLOW){
            action_field->data1 = GetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action) == 2;
        }
        else if (action_field->data0 == CTC_QOS_COLOR_RED){
            action_field->data1 = GetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action) == 2;
        }
        break;
        
    case CTC_ACL_FIELD_ACTION_DISCARD:
        if(action_field->data0 == CTC_QOS_COLOR_NONE)
        {
            action_field->data1 = (GetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action) == 1) \
                                || (GetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action) == 1) \
                                || (GetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action) == 1);
        }
        else if (action_field->data0 == CTC_QOS_COLOR_GREEN){
            action_field->data1 = GetDsEpeAcl(V, gDiscard_discardOpTypeGreen_f, p_buffer->action) == 1;
        }
        else if (action_field->data0 == CTC_QOS_COLOR_YELLOW){
            action_field->data1 = GetDsEpeAcl(V, gDiscard_discardOpTypeYellow_f, p_buffer->action) == 1;
        }
        else if (action_field->data0 == CTC_QOS_COLOR_RED){
            action_field->data1 = GetDsEpeAcl(V, gDiscard_discardOpTypeRed_f, p_buffer->action) == 1;
        }
        break;

    case CTC_ACL_FIELD_ACTION_CP_TO_CPU:
        CTC_PTR_VALID_CHECK(action_field->ext_data);
        p_cp_to_cpu = (ctc_acl_to_cpu_t*)action_field->ext_data;

        data1 = GetDsEpeAcl(V, gExcep_exceptionToCpuType_f, p_buffer->action);
        action_field->data1 = data1;
        if(1 == data1)
        {
            p_cp_to_cpu->mode = CTC_ACL_TO_CPU_MODE_TO_CPU_COVER;
        }
        else if(2 == data1)
        {
            p_cp_to_cpu->mode = CTC_ACL_TO_CPU_MODE_CANCEL_TO_CPU;
        }
        else if(3 == data1)
        {
            p_cp_to_cpu->mode = CTC_ACL_TO_CPU_MODE_TO_CPU_NOT_COVER;
        }
        p_cp_to_cpu->cpu_reason_id = p_buffer->ad.tcam.cpu_reason_id;
        break;

    case CTC_ACL_FIELD_ACTION_CANCEL_IPFIX:
        if (DRV_FROM_AT(lchip))
        {
            action_field->data0 = GetDsEpeAcl(V, ipfixOpCode_f, p_buffer->action) == 2? 1: 0;
        }
        else
        {
            action_field->data0 = GetDsEpeAcl(V, disableIpfix_f, p_buffer->action);
        }
        break;
    case CTC_ACL_FIELD_ACTION_COS_HBWP_POLICER:
    case CTC_ACL_FIELD_ACTION_MICRO_FLOW_POLICER:
    case CTC_ACL_FIELD_ACTION_MACRO_FLOW_POLICER:
    case CTC_ACL_FIELD_ACTION_COPP:
        SYS_USW_ACL_CHECK_ACTION_POLICER_MODE(pe, SYS_ACL_ACTION_POLICER_MODE_MICRO_FLOW + action_field->type - CTC_ACL_FIELD_ACTION_MICRO_FLOW_POLICER);
        action_field->data0 = GetDsEpeAcl(V, gPolicer_policerPtr_f, p_buffer->action);
        action_field->data1 = pe->cos_index;
        break;

    case CTC_ACL_FIELD_ACTION_TRUNCATED_LEN:
        data1 = GetDsEpeAcl(V, truncateLenProfId_f, p_buffer->action);
        if (data1)
        {
            sal_memset(&trunc_prof, 0, sizeof(DsTruncationProfile_m));
            cmd = DRV_IOR(DsTruncationProfile_t, DRV_ENTRY_FLAG);
            CTC_ERROR_RETURN(DRV_IOCTL(lchip, data1, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &trunc_prof));
            shift = GetDsTruncationProfile(V, lengthShift_f, &trunc_prof);
            length = GetDsTruncationProfile(V, length_f, &trunc_prof);
            length = length << shift;
            action_field->data0 = length;
        }
        break;

    case CTC_ACL_FIELD_ACTION_STATS:
        action_field->data0 = p_buffer->ad.tcam.stats_id;
        if(action_field->data0)
            action_field->data1 = GetDsEpeAcl(V, statsCareDiscard_f, p_buffer->action) ? 0 : 1;
        break;

    case CTC_ACL_FIELD_ACTION_VLAN_EDIT:
        CTC_PTR_VALID_CHECK(action_field->ext_data);
        p_ctc_vlan_edit = (ctc_acl_vlan_edit_t*)(action_field->ext_data);

        op = GetDsEpeAcl(V, gSvlanAction_sTagAction_f, p_buffer->action);
        mo = GetDsEpeAcl(V, gSvlanAction_stagModifyMode_f, p_buffer->action);
        _sys_usw_acl_vlan_tag_op_translate_reverse(lchip, op, mo, &p_ctc_vlan_edit->stag_op);
        if (CTC_ACL_VLAN_TAG_OP_NONE!= p_ctc_vlan_edit->stag_op)
        {
            p_ctc_vlan_edit->svid_sl = GetDsEpeAcl(V, gSvlanAction_sVlanIdAction_f, p_buffer->action);
            p_ctc_vlan_edit->scos_sl = GetDsEpeAcl(V, gSvlanAction_sCosAction_f, p_buffer->action);
            p_ctc_vlan_edit->scfi_sl = GetDsEpeAcl(V, gSvlanAction_sCfiAction_f, p_buffer->action);
            p_ctc_vlan_edit->tpid_index = GetDsEpeAcl(V, gSvlanAction_svlanTpidIndexEn_f, p_buffer->action) ? GetDsEpeAcl(V, gSvlanAction_svlanTpidIndex_f, p_buffer->action) : 0xff;
            p_ctc_vlan_edit->svid_new = GetDsEpeAcl(V, gSvlanAction_svlanId_f, p_buffer->action);
            p_ctc_vlan_edit->scos_new = GetDsEpeAcl(V, gSvlanAction_scos_f, p_buffer->action);
            p_ctc_vlan_edit->scfi_new = GetDsEpeAcl(V, gSvlanAction_scfi_f, p_buffer->action);
        }

        op = GetDsEpeAcl(V, u2_gCvlanAction_cTagAction_f, p_buffer->action);
        mo = GetDsEpeAcl(V, u2_gCvlanAction_ctagModifyMode_f, p_buffer->action);
        _sys_usw_acl_vlan_tag_op_translate_reverse(lchip, op, mo, &p_ctc_vlan_edit->ctag_op);
        if (CTC_ACL_VLAN_TAG_OP_NONE!= p_ctc_vlan_edit->ctag_op)
        {
            p_ctc_vlan_edit->cvid_sl = GetDsEpeAcl(V, u2_gCvlanAction_cVlanIdAction_f, p_buffer->action);
            p_ctc_vlan_edit->ccos_sl = GetDsEpeAcl(V, u2_gCvlanAction_cCosAction_f, p_buffer->action);
            p_ctc_vlan_edit->ccfi_sl = GetDsEpeAcl(V, u2_gCvlanAction_cCfiAction_f, p_buffer->action);
            p_ctc_vlan_edit->cvid_new = GetDsEpeAcl(V, u2_gCvlanAction_cvlanId_f, p_buffer->action);
            p_ctc_vlan_edit->ccos_new = GetDsEpeAcl(V, u2_gCvlanAction_ccos_f, p_buffer->action);
            p_ctc_vlan_edit->ccfi_new = GetDsEpeAcl(V, u2_gCvlanAction_ccfi_f, p_buffer->action);
        }
        action_field->data1 = (p_ctc_vlan_edit->ctag_op || p_ctc_vlan_edit->stag_op);
        break;      /*egress vlan edit end*/

    case CTC_ACL_FIELD_ACTION_RANDOM_LOG:
        log_id = GetDsEpeAcl(V, gLog_aclLogId_f, p_buffer->action);
        percent = GetDsEpeAcl(V, gLog_randomThresholdShift_f, p_buffer->action);

        if (action_field->ext_data)
        {
            p_log = (ctc_acl_log_t*)(action_field->ext_data);

            p_log->session_id = (log_id&0x3);
            p_log->log_id = (log_id >> 2);
            p_log->percent = percent;
        }
        else
        {
            action_field->data1 = percent;
            action_field->data0 = log_id;
        }
        break;

    case CTC_ACL_FIELD_ACTION_CANCEL_DOT1AE:
        if(DRV_FROM_AT(lchip))
        {
            action_field->data0 = GetDsEpeAcl(V, gXSec_bypassMacsecProc_f, p_buffer->action);
        }
        else
        {
            action_field->data0 = GetDsEpeAcl(V, gMacSec_bypassMacsecProc_f, p_buffer->action);
        }

        break;
	case CTC_ACL_FIELD_ACTION_ECN:
        if(DRV_IS_TMG(lchip))
        {
		    action_field->data0 = GetDsEpeAcl(V, gMacSec_transmitScId_f, p_buffer->action);
		    shift = GetDsEpeAcl(V, gMacSec_secTagOffsetType_f, p_buffer->action);
		    if(shift == 1)
		    {
			    action_field->data0 |= (1<<6); 
		    }
        }
        break;

    default:
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, " Feature not supported \n");
        return CTC_E_NOT_SUPPORT;

    }
    return CTC_E_NONE;
}

#define __ACL_UDF_ENTRY__
int32
_sys_tmm_acl_udf_entry_check(uint8 lchip,  ctc_acl_classify_udf_t* p_udf_entry)
{
    uint8  zero[CTC_ACL_UDF_BYTE_NUM] = {0};    /*must use the max of CTC_ACL_UDF_BYTE_NUM and CTC_ACL_UDF_FIELD_NUM*/
    uint8  remainder = 0;
    uint8  offset_len = 0;
    uint8  loop_i = 0;
    ctc_acl_classify_udf_t* p_udf_entry_next = NULL;

    if(p_udf_entry->sudf_en && (p_udf_entry->scl_key || p_udf_entry->ipfix_en))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "If sudf_en enable, not support scl-key or ipfix udf!\n");
        return CTC_E_INVALID_PARAM;
    }

    if(!p_usw_acl_master[lchip]->udf_fpa_en && 0 == p_udf_entry->sudf_en)/* priority 511(index 0) reserved ,ti spec bug */
    {
        CTC_MAX_VALUE_CHECK(p_udf_entry->priority, MCHIP_CAP(SYS_CAP_ACL_UDF_ENTRY_NUM)-1);
    }
    CTC_MAX_VALUE_CHECK(p_udf_entry->offset_type, CTC_ACL_UDF_TYPE_NUM - 1);
    CTC_MAX_VALUE_CHECK(p_udf_entry->granularity, 1);
    if(CTC_ACL_UDF_TYPE_NONE == p_udf_entry->offset_type && !sal_memcmp(p_udf_entry->offset_type_array, zero, CTC_ACL_UDF_FIELD_NUM))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "UDF offset type is invalid!\n");
        return CTC_E_INVALID_PARAM;
    }
    if (p_udf_entry->granularity)
    {
        CTC_VALUE_RANGE_CHECK(p_udf_entry->offset_num, 1, MCHIP_CAP(SYS_CAP_ACL_UDF_OFFSET_NUM));
        offset_len = 2;
    }
    else
    {
        CTC_VALUE_RANGE_CHECK(p_udf_entry->offset_num, 1, MCHIP_CAP(SYS_CAP_ACL_UDF_OFFSET_NUM)/2);
        offset_len = 4;
    }
    for (loop_i = 0; loop_i < p_udf_entry->offset_num; loop_i++)
    {
        CTC_MAX_VALUE_CHECK(p_udf_entry->offset_type_array[loop_i], CTC_ACL_UDF_TYPE_NUM - 1);
        CTC_MAX_VALUE_CHECK(p_udf_entry->offset[loop_i] /offset_len, MCHIP_CAP(SYS_CAP_ACL_UDF_OFFSET_MAX));
        remainder = remainder || (p_udf_entry->offset[loop_i] % offset_len);
    }
    if (remainder)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "The offset :offset[0]:%u", p_udf_entry->offset[0]);
        for (loop_i = 1; loop_i < p_udf_entry->offset_num; loop_i++)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, ",offset[%u]:%u", loop_i, p_udf_entry->offset[loop_i]);
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "\n");
        return CTC_E_INVALID_PARAM;
    }
    /*check scl_key*/
    p_udf_entry_next = p_udf_entry->scl_key;
    if (p_udf_entry_next)  /*non service udf check*/
    {
        CTC_VALUE_RANGE_CHECK(p_udf_entry_next->offset_num, 1, (p_udf_entry->granularity ? MCHIP_CAP(SYS_CAP_ACL_UDF_OFFSET_NUM) : MCHIP_CAP(SYS_CAP_ACL_UDF_OFFSET_NUM)/2));
        remainder = 0;
        for (loop_i = 0; loop_i < p_udf_entry_next->offset_num; loop_i++)
        {
            CTC_VALUE_RANGE_CHECK(p_udf_entry_next->offset_type_array[loop_i], CTC_ACL_UDF_TYPE_NONE + 1, CTC_ACL_UDF_TYPE_NUM - 1);
            CTC_MAX_VALUE_CHECK(p_udf_entry_next->offset[loop_i] /offset_len, MCHIP_CAP(SYS_CAP_ACL_UDF_OFFSET_MAX));
            remainder = remainder || (p_udf_entry_next->offset[loop_i] % offset_len);
        }
        if (remainder)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "The offset :offset[0]:%u", p_udf_entry_next->offset[0]);
            for (loop_i = 1; loop_i < p_udf_entry_next->offset_num; loop_i++)
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, ",offset[%u]:%u", loop_i, p_udf_entry_next->offset[loop_i]);
            }
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "\n");
            return CTC_E_INVALID_PARAM;
        }
    }
    return CTC_E_NONE;
}

STATIC int32
  _sys_tmm_acl_map_ctc_to_sys_udf_info(uint8 lchip, ctc_acl_classify_udf_t* p_udf_entry)
{
    uint8  loop_i = 0;
    uint8  offset = 0;
    uint8  p_offset_type_array[CTC_ACL_UDF_FIELD_NUM];
    uint8  p_offset[CTC_ACL_UDF_FIELD_NUM];
    uint8  p_scl_offset_type_array[CTC_ACL_UDF_FIELD_NUM];
    uint8  p_scl_offset[CTC_ACL_UDF_FIELD_NUM];

    CTC_PTR_VALID_CHECK(p_udf_entry);

    sal_memcpy(p_offset_type_array, p_udf_entry->offset_type_array, CTC_ACL_UDF_FIELD_NUM * sizeof(uint8));
    sal_memcpy(p_offset, p_udf_entry->offset, CTC_ACL_UDF_FIELD_NUM * sizeof(uint8));
    if(!p_udf_entry->granularity)
    {
        p_udf_entry->offset_num = p_udf_entry->offset_num <<1;
        for (loop_i = 0; loop_i < CTC_ACL_UDF_FIELD_NUM; loop_i++)
        {
            if (CTC_ACL_UDF_TYPE_NONE != p_udf_entry->offset_type)
            {
                p_udf_entry->offset_type_array[loop_i] = p_udf_entry->offset_type;
            }
            else
            {
                p_udf_entry->offset_type_array[loop_i] = p_offset_type_array[loop_i / 2];
            }
            offset = (p_offset[loop_i / 2]  + (loop_i % 2)*2);
            p_udf_entry->offset[loop_i] = offset;
        }
        if (p_udf_entry->scl_key)
        {
            sal_memcpy(p_scl_offset_type_array, p_udf_entry->scl_key->offset_type_array, CTC_ACL_UDF_FIELD_NUM * sizeof(uint8));
            sal_memcpy(p_scl_offset, p_udf_entry->scl_key->offset, CTC_ACL_UDF_FIELD_NUM * sizeof(uint8));
            p_udf_entry->scl_key->offset_num= p_udf_entry->scl_key->offset_num <<1;
            for (loop_i = 0; loop_i < CTC_ACL_UDF_FIELD_NUM; loop_i++)
            {
                if (CTC_ACL_UDF_TYPE_NONE != p_udf_entry->scl_key->offset_type)
                {
                    p_udf_entry->scl_key->offset_type_array[loop_i] = p_udf_entry->scl_key->offset_type;
                }
                else
                {
                    p_udf_entry->scl_key->offset_type_array[loop_i] = p_scl_offset_type_array[loop_i / 2];
                }
                offset = (p_scl_offset[loop_i / 2]  + (loop_i % 2)*2);
                p_udf_entry->scl_key->offset[loop_i] = offset;
            }
        }

    }
    return CTC_E_NONE;
}

void
_sys_tmm_acl_build_udf_cam_action(uint8 lchip, uint8 offset_num, uint8 udf_cam_type, uint8 gran_valid, ctc_acl_classify_udf_t* p_udf_entry, void* ds)
{
    uint8* p_udf_mask = NULL;
    uint32 hw_udf_mask[4] = {0};
    uint32 offset = 0;
    uint8  loop_i = 0;
    uint8  acl_offset_base = SYS_ACL_UDF_HALF_FIELD_NUM;
    uint8  loop_start = 0;
    uint8  loop_end = 0;
    uint8  loop_tmp = 0;
    uint8  offset_type = 0;
    uint8  udf_mask_full[CTC_ACL_UDF_BYTE_NUM];

    sal_memset(udf_mask_full, 0xFF, sizeof(udf_mask_full));
    loop_end = (offset_num > acl_offset_base)?  acl_offset_base : offset_num;
    {/*udf mask process*/
    p_udf_mask = p_udf_entry->mask;
    if (!sal_memcmp(p_udf_mask, hw_udf_mask, sizeof(hw_udf_mask)))
    {
        SetParserUdfCamResult(A, udfMask_f, ds, udf_mask_full);
    }
    else
    {
        SYS_USW_SET_HW_UDF(gran_valid, hw_udf_mask, p_udf_mask);
        SetParserUdfCamResult(A, udfMask_f, ds, hw_udf_mask);
    }
    }
    offset_type = sys_usw_acl_map_ctc_to_sys_offset_type(lchip, p_udf_entry->offset_type);
    for (loop_i = loop_start; loop_i < loop_end; loop_i++)
    {
        loop_tmp = gran_valid ? loop_i : ((loop_i%2) ? loop_i - 1 : loop_i + 1);
        if (CTC_ACL_UDF_TYPE_NONE == p_udf_entry->offset_type)
        {
            offset_type = sys_usw_acl_map_ctc_to_sys_offset_type(lchip, p_udf_entry->offset_type_array[loop_tmp]);
        }
        offset = p_udf_entry->offset[loop_tmp] / MCHIP_CAP(SYS_CAP_ACL_UDF_OFFSET_BYTE_LEN);
        SetParserUdfCamResult(V, udfStartPosType0_f + loop_i, ds, offset_type); /* First: StartPosType */
        SetParserUdfCamResult(V, udfEntryOffset0_f + loop_i, ds, offset);           /* Second: offset */
    }
}

int32
sys_tmm_acl_enable_udf_ipfix(uint8 lchip, uint16 udf_hit_idx,uint8 enable)
{
    IpeUdfIpfixCtl_m   udf_ipfix_ctl;
    uint16 hit_index;
    uint32 cmd = 0;    
    uint32   udfa0mask[4] = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};

    cmd = DRV_IOR(IpeUdfIpfixCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &udf_ipfix_ctl));

    if(!enable)
    {
        hit_index = GetIpeUdfIpfixCtl(V, udfIpfixMatchIndexA0_f, &udf_ipfix_ctl);
        if(hit_index!= udf_hit_idx || !GetIpeUdfIpfixCtl(V, udfIpfixMatchIndexA0Valid_f , &udf_ipfix_ctl))
            return CTC_E_NONE ;
        udf_hit_idx = 0;
    }
    SetIpeUdfIpfixCtl(V, udfIpfixMatchIndexA0_f , &udf_ipfix_ctl, udf_hit_idx);
    SetIpeUdfIpfixCtl(V, udfIpfixMatchIndexA0Valid_f , &udf_ipfix_ctl, !!enable);
    SetIpeUdfIpfixCtl(V, udfValidA0Mask_f , &udf_ipfix_ctl, 0xFF);
    SetIpeUdfIpfixCtl(A, udfA0Mask_f , &udf_ipfix_ctl, udfa0mask);
    cmd = DRV_IOW( IpeUdfIpfixCtl_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, 0, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &udf_ipfix_ctl));

    return CTC_E_NONE;
}

void
sys_tmm_acl_build_udf_cam_action(uint8 lchip, sys_acl_udf_entry_t* p_sys_udf_entry, ctc_acl_classify_udf_t* p_udf_entry, ParserUdfCamResult_m* udf_rst)
{
    /*common field*/
    SetParserUdfCamResult(V, skipTcpOption_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], p_udf_entry->tcp_option_en?1:0);
    SetParserUdfCamResult(V, skipVlanTag_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], p_udf_entry->skip_vlan?1:0);
    SetParserUdfCamResult(V, udfMappedIndex_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], p_sys_udf_entry->udf_hit_index);
    SetParserUdfCamResult(V, flowDigestUdfEn_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], 1);/*TODO*/

    SetParserUdfCamResult(V, aclUdfEn_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], (CTC_FLAG_ISSET(p_udf_entry->flag, CTC_ACL_CLASSIFY_UDF_ACL_XDATA)));
    #ifdef AT_TODO
    SetParserUdfCamResult(V, flowKeyUdfSelType_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], 3);/*TODO*/
    SetParserUdfCamResult(V, ipfixUdfSelType_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], 1);/*TODO*/
    SetParserUdfCamResult(V, flowDigestUdfSelType_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], 1);/*TODO*/
    SetParserUdfCamResult(V, udfAuxEn_f, &udf_rst[SYS_UDF_CAM_TYPE_FIB], 0);/*TODO*/
    #endif
    sal_memcpy(&udf_rst[SYS_UDF_CAM_TYPE_ACL], &udf_rst[SYS_UDF_CAM_TYPE_FIB], sizeof(ParserUdfCamResult_m));
    sal_memcpy(&udf_rst[SYS_UDF_CAM_TYPE_SCL], &udf_rst[SYS_UDF_CAM_TYPE_FIB], sizeof(ParserUdfCamResult_m));

    if (!p_udf_entry->scl_key)
    {
        _sys_tmm_acl_build_udf_cam_action(lchip, p_udf_entry->offset_num, SYS_UDF_CAM_TYPE_FIB, p_udf_entry->granularity, p_udf_entry, &udf_rst[SYS_UDF_CAM_TYPE_FIB]);
        _sys_tmm_acl_build_udf_cam_action(lchip, p_udf_entry->offset_num, SYS_UDF_CAM_TYPE_ACL, p_udf_entry->granularity, p_udf_entry, &udf_rst[SYS_UDF_CAM_TYPE_ACL]);
        sal_memcpy(&udf_rst[SYS_UDF_CAM_TYPE_SCL], &udf_rst[SYS_UDF_CAM_TYPE_FIB], sizeof(ParserUdfCamResult_m));
    }
    else
    {
        _sys_tmm_acl_build_udf_cam_action(lchip, p_udf_entry->offset_num, SYS_UDF_CAM_TYPE_FIB, p_udf_entry->granularity, p_udf_entry, &udf_rst[SYS_UDF_CAM_TYPE_FIB]);
        _sys_tmm_acl_build_udf_cam_action(lchip, p_udf_entry->offset_num, SYS_UDF_CAM_TYPE_ACL, p_udf_entry->granularity, p_udf_entry, &udf_rst[SYS_UDF_CAM_TYPE_ACL]);
        _sys_tmm_acl_build_udf_cam_action(lchip, p_udf_entry->scl_key->offset_num, SYS_UDF_CAM_TYPE_SCL, p_udf_entry->granularity, p_udf_entry->scl_key, &udf_rst[SYS_UDF_CAM_TYPE_SCL]);
    }
}


int32
sys_tmm_acl_add_udf_entry(uint8 lchip,  ctc_acl_classify_udf_t* p_udf_entry)
{
    sys_acl_udf_entry_t    *p_sys_udf_entry = NULL, *p_sys_udf_entry2 = NULL;
    ParserUdfCamResult_m udf_rst[SYS_UDF_CAM_TYPE_NUM];
    ParserUdfCam_m udf_cam_data;
    ParserUdfCam_m udf_cam_mask;
    sys_usw_opf_t   opf;
    uint32 offset = 0;
    uint32 old_offset = 0;
    uint8  is_add = 0;
    uint8  add_udf_vec = 0;
    uint8  udf_mask_full[CTC_ACL_UDF_BYTE_NUM];
    int32    ret = 0;
    sys_acl_traverse_data_t  user_data;
    tbl_entry_t hw_udf_entry;
    uint8 udf_mode = p_usw_acl_master[lchip]->udf_mode;
    sys_acl_udf_ad_t *p_sys_udf_ad = NULL;
    ctc_acl_classify_udf_t* p_origin_udf_entry =  p_udf_entry;
    ctc_acl_classify_udf_t udf_entry[2];

    sal_memset(&opf, 0, sizeof(opf));
    sal_memset(udf_mask_full, 0xFF, sizeof(udf_mask_full));
    sal_memset(&udf_cam_data, 0, sizeof(ParserUdfCam_m));
    sal_memset(&udf_cam_mask, 0, sizeof(ParserUdfCam_m));
    sal_memset(&user_data, 0, sizeof(sys_acl_traverse_data_t));
    sal_memset(udf_entry, 0, 2*sizeof(ctc_acl_classify_udf_t));

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    sal_memcpy(udf_entry,p_origin_udf_entry,sizeof(ctc_acl_classify_udf_t));
    p_udf_entry = udf_entry;

    if(p_origin_udf_entry->scl_key)
    {
        sal_memcpy(&udf_entry[1], p_origin_udf_entry->scl_key,sizeof(ctc_acl_classify_udf_t));
        p_udf_entry->scl_key = &udf_entry[1];
    }
    if (p_udf_entry->shared_udf && p_udf_entry->sudf_en)
    {
        p_udf_entry->udf_ad_id +=(MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MIN)-1);
        CTC_VALUE_RANGE_CHECK(p_udf_entry->udf_ad_id, MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MIN), MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MAX))
    }
    else if(p_udf_entry->shared_udf && !p_udf_entry->sudf_en)
    {
        CTC_VALUE_RANGE_CHECK(p_udf_entry->udf_ad_id, MCHIP_CAP(SYS_CAP_ACL_UDF_AD_ID_MIN), MCHIP_CAP(SYS_CAP_ACL_UDF_AD_ID_MAX))
    }

    hw_udf_entry.data_entry = (uint32*)&udf_cam_data;
    hw_udf_entry.mask_entry = (uint32*)&udf_cam_mask;
    CTC_ERROR_RETURN(_sys_tmm_acl_udf_entry_check(lchip, p_udf_entry));
    _sys_tmm_acl_map_ctc_to_sys_udf_info(lchip,p_udf_entry);
    CTC_ERROR_RETURN(_sys_usw_acl_get_udf_info(lchip,  p_udf_entry->udf_id, &p_sys_udf_entry));
    if(p_sys_udf_entry && p_sys_udf_entry->is_sudf != (!!p_udf_entry->sudf_en))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Service UDF Entry and UDF Entry can not Traslate each other: sudf_en:%d, is_sudf:%d\n", \
            p_udf_entry->sudf_en, p_sys_udf_entry->is_sudf);
        CTC_ERROR_RETURN(CTC_E_NOT_SUPPORT);
    }

    if(!p_usw_acl_master[lchip]->udf_fpa_en && 0 == p_udf_entry->sudf_en)
    {
        CTC_ERROR_RETURN(_sys_usw_acl_get_udf_entry_by_priority(lchip, p_udf_entry->priority, &p_sys_udf_entry2));
        if ((NULL == p_sys_udf_entry) /* add entry */
            ||(p_sys_udf_entry2 !=p_sys_udf_entry)) /* update entry, and update priority */
        {
            if (p_sys_udf_entry2 && p_sys_udf_entry2->key_index_used) /* !flow_entry, chk priority used*/
            {
                CTC_ERROR_RETURN(CTC_E_EXIST);
            }
        }
    }

    opf.pool_type  = p_usw_acl_master[lchip]->opf_type_udf;
    opf.multiple = 0;

    if(NULL == p_sys_udf_entry) /* add */
    {
        is_add = 1;
        /*malloc new entry*/
        MALLOC_ZERO(MEM_ACL_MODULE, p_sys_udf_entry, sizeof(sys_acl_udf_entry_t));
        if (NULL == p_sys_udf_entry)
        {
            CTC_ERROR_RETURN(CTC_E_NO_MEMORY);
        }

        if(p_udf_entry->sudf_en)
        {
            offset = 0;
            opf.pool_index = SYS_USW_ACL_SUDF_AD_INDEX;  /* service udf alloc > 512,  return no resource, should not alloc hit_index, this order not to error rollback*/
            ret =  sys_usw_opf_alloc_offset(lchip, &opf, 1, &offset);
            if (ret < 0)
            {
                CTC_ERROR_GOTO(CTC_E_NO_RESOURCE,ret,error0);
            }
            p_sys_udf_entry->key_index = offset; /* is_sudf means ad_index*/
            p_sys_udf_entry->is_sudf = 1;
        }

        if (!p_udf_entry->shared_udf)
        {
            opf.pool_index = p_udf_entry->sudf_en? SYS_USW_ACL_UDF_MAPPED_INDEX1: SYS_USW_ACL_UDF_MAPPED_INDEX;
            ret =  sys_usw_opf_alloc_offset(lchip, &opf, 1, &offset);
            if (ret < 0)
            {
                CTC_ERROR_GOTO(CTC_E_NO_RESOURCE,ret,error1);
            }
            p_sys_udf_entry->udf_hit_index = offset;
            p_origin_udf_entry->udf_ad_id = p_udf_entry->sudf_en? (offset-MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MIN) + 1): offset;
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "%s alloc udf mapped index: %u\n", p_sys_udf_entry->is_sudf ? "sudf" : "", p_sys_udf_entry->udf_hit_index);
        }
        else
        {
            p_sys_udf_entry->udf_hit_index = p_udf_entry->udf_ad_id;
        }

        p_sys_udf_ad = ctc_vector_get(p_usw_acl_master[lchip]->udf_ad, p_sys_udf_entry->udf_hit_index);
        if(!p_sys_udf_ad)
        {
            add_udf_vec = 1;
            MALLOC_ZERO(MEM_ACL_MODULE, p_sys_udf_ad, sizeof(sys_acl_udf_ad_t));
            if (NULL == p_sys_udf_ad)
            {
                CTC_ERROR_GOTO(CTC_E_NO_RESOURCE,ret,error2)
            }
            if (!ctc_vector_add(p_usw_acl_master[lchip]->udf_ad, p_sys_udf_entry->udf_hit_index, p_sys_udf_ad))
            {
                ret = CTC_E_NO_MEMORY;
                goto error3;
            }
            if(p_udf_entry->shared_udf)
            {
                opf.pool_index = p_udf_entry->sudf_en? SYS_USW_ACL_UDF_MAPPED_INDEX1: SYS_USW_ACL_UDF_MAPPED_INDEX;
                CTC_ERROR_GOTO(sys_usw_opf_alloc_offset_from_position(lchip, &opf, 1, p_udf_entry->udf_ad_id),ret,error4)/*srv6 reserved udf_ad_id not add vector,user use this udf_ad_id will return entry exist*/
            }
        }
        p_sys_udf_ad->ref_cnt++;

    }
    else
    {
        if(p_udf_entry->shared_udf && p_sys_udf_entry->udf_hit_index != p_udf_entry->udf_ad_id)/*not support update udf_ad_id*/
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "not support update udf_ad_id");
            CTC_ERROR_RETURN(CTC_E_INVALID_PARAM);
        }
        p_origin_udf_entry->udf_ad_id =p_sys_udf_entry->is_sudf? (p_sys_udf_entry->udf_hit_index-MCHIP_CAP(SYS_CAP_ACL_SUDF_AD_ID_MIN) + 1): p_sys_udf_entry->udf_hit_index;/*update,return have allocated udf_ad_id*/
    }

    old_offset = p_sys_udf_entry->key_index;    /*for update*/



    if(!p_udf_entry->sudf_en)
    {
        if(p_usw_acl_master[lchip]->udf_fpa_en)
        {
            if(is_add)
            {
                uint32 block_index;
                CTC_ERROR_GOTO(fpa_usw_alloc_offset(p_usw_acl_master[lchip]->udf_fpa, &p_usw_acl_master[lchip]->udf_block.fpab, CTC_FPA_KEY_SIZE_80, p_udf_entry->priority, &block_index), ret, error5);

                p_sys_udf_entry->key_index = block_index;
                p_sys_udf_entry->fpae.offset_a = block_index;
                p_sys_udf_entry->fpae.priority = p_udf_entry->priority;
                p_usw_acl_master[lchip]->udf_block.fpab.entries[block_index] = &p_sys_udf_entry->fpae;
            }
            else if(p_sys_udf_entry->fpae.priority != p_udf_entry->priority)
            {
                CTC_ERROR_GOTO(fpa_usw_set_entry_prio(p_usw_acl_master[lchip]->udf_fpa, &p_sys_udf_entry->fpae, &p_usw_acl_master[lchip]->udf_block.fpab, CTC_FPA_KEY_SIZE_80, p_udf_entry->priority), ret, error5);
            }
        }
        else
        {
            p_sys_udf_entry->key_index = SYS_ACL_UDF_PRI_2_IDX(p_udf_entry->priority);
        }
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "udf entry key index: %u\n", p_sys_udf_entry->key_index);
        if(DRV_FROM_AT(lchip) && udf_mode)/*udf tmm mode in arctic */
        {
            opf.pool_index = SYS_USW_ACL_SUDF_AD_INDEX;
            offset = MCHIP_CAP(SYS_CAP_ACL_UDF_ENTRY_NUM)+ p_sys_udf_entry->key_index;
            ret =  sys_usw_opf_alloc_offset_from_position(lchip, &opf, 1, offset );
            if (ret < 0)
            {
                CTC_ERROR_GOTO(CTC_E_INVALID_PARAM,ret,error5);
            }
        }
    }

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "is_sudf: %u, udf entry ad index: %u\n", p_sys_udf_entry->is_sudf, p_sys_udf_entry->key_index);

    p_sys_udf_entry->udf_offset_num = p_udf_entry->offset_num > CTC_ACL_UDF_FIELD_NUM?CTC_ACL_UDF_FIELD_NUM:p_udf_entry->offset_num;
    p_sys_udf_entry->scl_offset_num = p_udf_entry->scl_key ? p_udf_entry->scl_key->offset_num : p_sys_udf_entry->udf_offset_num;

    if (p_sys_udf_entry->scl_offset_num > CTC_ACL_UDF_FIELD_NUM)
    {
        p_sys_udf_entry->scl_offset_num = CTC_ACL_UDF_FIELD_NUM;
    }
    p_sys_udf_entry->scl_key_valid = p_udf_entry->scl_key? 1: 0;
    sal_memset(udf_rst, 0, SYS_UDF_CAM_TYPE_NUM * sizeof(ParserUdfCamResult_m));
    sys_tmm_acl_build_udf_cam_action(lchip, p_sys_udf_entry, p_udf_entry, udf_rst);
    CTC_ERROR_GOTO(sys_usw_parser_io_set_parser_udf_cam_result(lchip, udf_rst,p_sys_udf_entry->key_index, p_udf_entry->sudf_en, udf_mode),ret,error6);
    p_sys_udf_entry->udf_id = p_udf_entry->udf_id;
    p_sys_udf_entry->granularity = p_udf_entry->granularity; /* !granularity update t0 granularity */
    p_sys_udf_entry->key_index_used = 1;
    p_sys_udf_entry->offset_type_valid = p_udf_entry->offset_type ? 1 : 0;
    if((!p_sys_udf_entry->is_sudf) && (is_add)) /* normal udf and add entry */
    {
        MALLOC_ZERO(MEM_ACL_MODULE, p_sys_udf_entry->buffer, sizeof(sys_acl_udf_buffer_t));
        if (NULL == p_sys_udf_entry->buffer)
        {
            ret = CTC_E_NO_MEMORY;
            goto error6;
        }
    }

    /* add to hash */
    if(is_add)
    {
        ctc_hash_insert(p_usw_acl_master[lchip]->udf_entry, p_sys_udf_entry);
        SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_UDF_ENTRY, 1);
    }
    else if (!p_usw_acl_master[lchip]->udf_fpa_en && old_offset != p_sys_udf_entry->key_index)  /*remove old udf*/
    {
        uint32 cmd = DRV_IOR(ParserUdfCam_t, DRV_ENTRY_FLAG);
        CTC_ERROR_GOTO(DRV_IOCTL(lchip, old_offset, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &hw_udf_entry), ret, error5);
        cmd = DRV_IOW(ParserUdfCam_t, DRV_ENTRY_FLAG);
        CTC_ERROR_GOTO(DRV_IOCTL(lchip, p_sys_udf_entry->key_index, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &hw_udf_entry), ret, error5);
        cmd = DRV_IOD(ParserUdfCam_t, DRV_ENTRY_FLAG);
        CTC_ERROR_GOTO(DRV_IOCTL(lchip, old_offset, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &hw_udf_entry), ret, error5);
        sal_memset(udf_rst, 0, SYS_UDF_CAM_TYPE_NUM * sizeof(ParserUdfCamResult_m));
        CTC_ERROR_GOTO(sys_usw_parser_io_set_parser_udf_cam_result(lchip, udf_rst, old_offset, 0, udf_mode),ret,error6);/*must not sudf*/
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "udf entry update from %u to %u\n", old_offset, p_sys_udf_entry->key_index);
        if(DRV_FROM_AT(lchip) && udf_mode)/*tmm mode,update !sudf, release  old opf offset*/
        {
            opf.pool_type  = p_usw_acl_master[lchip]->opf_type_udf;
            opf.multiple = 0;
            opf.pool_index = SYS_USW_ACL_SUDF_AD_INDEX;
            offset = MCHIP_CAP(SYS_CAP_ACL_UDF_ENTRY_NUM)+ old_offset;
            sys_usw_opf_free_offset(lchip,&opf, 1, offset);
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "free sudf ad index: %u\n", offset);
        }
    }
    if((DRV_IS_TMM(lchip) || DRV_IS_TMG(lchip)))
    {
        sys_tmm_acl_enable_udf_ipfix(lchip,p_sys_udf_entry->udf_hit_index,p_udf_entry->ipfix_en); 
    }
    return CTC_E_NONE;
error6:
    if(!p_udf_entry->sudf_en && udf_mode)
    {
        opf.pool_index = SYS_USW_ACL_SUDF_AD_INDEX;
        offset = MCHIP_CAP(SYS_CAP_ACL_UDF_ENTRY_NUM)+ p_sys_udf_entry->key_index;
        sys_usw_opf_free_offset(lchip, &opf, 1, offset);
    }
error5:
    if (is_add)
    {
        p_sys_udf_ad->ref_cnt--;
        if(add_udf_vec && p_udf_entry->shared_udf)
        {
            opf.pool_index = (p_sys_udf_entry->udf_hit_index <= MCHIP_CAP(SYS_CAP_ACL_UDF_ENTRY_NUM))? SYS_USW_ACL_UDF_MAPPED_INDEX: SYS_USW_ACL_UDF_MAPPED_INDEX1;
            sys_usw_opf_free_offset(lchip, &opf, 1, p_origin_udf_entry->udf_ad_id);
        }
    }
error4:
    if(is_add && add_udf_vec)
    {
        ctc_vector_del(p_usw_acl_master[lchip]->udf_ad, p_sys_udf_entry->udf_hit_index);
    }
error3:
    if(is_add && add_udf_vec)
    {
        mem_free(p_sys_udf_ad);
    }
error2:
    if(is_add && !p_udf_entry->shared_udf)
    {
        opf.pool_index = p_udf_entry->sudf_en? SYS_USW_ACL_UDF_MAPPED_INDEX1: SYS_USW_ACL_UDF_MAPPED_INDEX;
        sys_usw_opf_free_offset(lchip, &opf, 1, p_sys_udf_entry->udf_hit_index);
    }
error1:
    if(is_add && p_udf_entry->sudf_en)
    {
        opf.pool_index = SYS_USW_ACL_SUDF_AD_INDEX;
        sys_usw_opf_free_offset(lchip, &opf, 1, p_sys_udf_entry->key_index);
    }
error0:
    if(is_add && p_sys_udf_entry)
    {
        mem_free(p_sys_udf_entry);
    }
    return ret;
}

int32
_sys_tmm_acl_wb_mapping_presel(uint8 lchip, sys_wb_acl_presel_t* p_wb_acl_presel, sys_acl_presel_t* p_acl_presel, uint8 is_sync)
{
    uint8  loop = 0;
    uint8  block_base = 0;
    tbl_entry_t presel_entry;
    uint32 presel_index = 0;
    uint32 cmd = 0;
    sys_acl_lt_block_t* p_ltb = NULL;
    if (is_sync)
    {
        p_wb_acl_presel->presel_id = p_acl_presel->presel_id;
        p_wb_acl_presel->flag = p_acl_presel->flag;
        p_wb_acl_presel->key_type = p_acl_presel->fk->key_type;
        if(SYS_ACL_FK_IS_HASH(p_acl_presel->fk))
        {
            p_wb_acl_presel->lkup_level = p_acl_presel->lkup_level;
            p_wb_acl_presel->fpa_flag = p_acl_presel->fpae.flag;
        }
        else
        {
            p_wb_acl_presel->priority = p_acl_presel->fpae.priority;
            p_wb_acl_presel->fpa_flag = p_acl_presel->fpae.flag;
            p_wb_acl_presel->fpa_step = p_acl_presel->fpae.step;
            p_wb_acl_presel->fpa_offset_a = p_acl_presel->fpae.offset_a;
            p_wb_acl_presel->lkup_level = p_acl_presel->pltb->block_id;
        }
    }
    else
    {
        p_acl_presel->presel_id = p_wb_acl_presel->presel_id;
        p_acl_presel->flag = p_wb_acl_presel->flag;
        p_acl_presel->lkup_level = p_wb_acl_presel->lkup_level;
        p_acl_presel->dir = SYS_ACL_BLOCK_DIR(p_wb_acl_presel->lkup_level);
        if (MCHIP_ACL(lchip)->get_flex_key)
        {
            p_acl_presel->fk = MCHIP_ACL(lchip)->get_flex_key(lchip, p_acl_presel->dir, p_wb_acl_presel->key_type);
        }
        if(SYS_ACL_FK_IS_HASH(p_acl_presel->fk))
        {
            p_acl_presel->dir = CTC_INGRESS;
            p_acl_presel->fpae.flag = p_wb_acl_presel->fpa_flag;
            ((sys_acl_flex_hash_t *)p_acl_presel->fk)->presel_cnt++;
            return CTC_E_NONE;
        }
        
        block_base = SYS_ACL_BLOCK_BASE(p_wb_acl_presel->lkup_level);
        p_ltb = &p_usw_acl_master[lchip]->lt_block[p_wb_acl_presel->lkup_level];
        p_acl_presel->pltb = p_ltb;
        
        p_acl_presel->fpae.priority = p_wb_acl_presel->priority;
        p_acl_presel->fpae.flag = p_wb_acl_presel->fpa_flag;
        p_acl_presel->fpae.step = p_wb_acl_presel->fpa_step;
        p_acl_presel->fpae.offset_a = p_wb_acl_presel->fpa_offset_a;
        p_acl_presel->fpae.entry_id = p_wb_acl_presel->presel_id;
        
        p_ltb->fpab.entries[p_wb_acl_presel->fpa_offset_a] = &(p_acl_presel->fpae);
        p_ltb->fpab.free_count--;
        p_ltb->fpab.sub_free_count[0]--;

        if (p_usw_acl_master[lchip]->glb_presel_en)
        {
            MALLOC_ZERO(MEM_ACL_MODULE, p_acl_presel->buffer, sizeof(sys_acl_buffer_t))
            if(!p_acl_presel->buffer)
            {
                return CTC_E_NO_MEMORY;
            }
            p_acl_presel->fk->presel_cnt++;
            presel_entry.data_entry = p_acl_presel->buffer->key;
            presel_entry.mask_entry = p_acl_presel->buffer->mask;
            cmd = DRV_IOR(DsLtidSelectTcam_t, DRV_ENTRY_FLAG);
            SYS_ACL_ENCODE_PRESEL_TCAM_INDEX(p_wb_acl_presel->lkup_level, p_wb_acl_presel->fpa_offset_a, presel_index);
            DRV_IOCTL(lchip, presel_index, DRV_CMD_PP_EN(cmd), &presel_entry);
        }
        else
        {
            CTC_BIT_SET(p_acl_presel->fk->presel_cnt, p_wb_acl_presel->lkup_level-block_base);
            _sys_tmm_acl_presel_expand_lt_block(lchip, p_wb_acl_presel->lkup_level, p_acl_presel->fk->key_size);
        }
        if (p_acl_presel->fk && !CTC_FLAG_ISSET(p_acl_presel->flag, CTC_ACL_PRESEL_FLAG_LKUP_LEVEL))
        {
            for (loop=0; loop<SYS_ACL_PRESEL_NUM; loop++)
            {
                if (!p_acl_presel->fk->presel_ent_arr[loop])
                {
                    break;
                }
            }
            if (loop < SYS_ACL_PRESEL_NUM)
            {
                p_acl_presel->fk->presel_ent_arr[loop] = p_acl_presel;
            }
        }
        p_acl_presel->fk->ad_index = p_acl_presel->fpae.offset_a;
    }
    return CTC_E_NONE;
}

int32
_sys_tmm_acl_wb_sync_presel(void* bucket_data, void* user_data)
{
    uint32 max_entry_cnt = 0;
    ctc_wb_data_t* p_wb_data = NULL;
    sys_acl_presel_t* p_acl_presel = NULL;
    sys_wb_acl_presel_t* p_wb_acl_presel = NULL;
    p_wb_data = (ctc_wb_data_t*)(((sys_acl_traverse_data_t*)user_data)->data0);
    max_entry_cnt = p_wb_data->buffer_len/ sizeof(sys_wb_acl_presel_t);

    p_acl_presel = (sys_acl_presel_t*)bucket_data;
    p_wb_acl_presel = (sys_wb_acl_presel_t*)p_wb_data->buffer + p_wb_data->valid_cnt;

    _sys_tmm_acl_wb_mapping_presel(0, p_wb_acl_presel, p_acl_presel, 1);

    if (++p_wb_data->valid_cnt == max_entry_cnt)
    {
        CTC_ERROR_RETURN(ctc_wb_add_entry(p_wb_data));
        p_wb_data->valid_cnt = 0;
    }
    return CTC_E_NONE;
}

int32
sys_tmm_acl_wb_sync(uint8 lchip, uint32 app_id, ctc_wb_data_t* p_wb_data, sys_acl_traverse_data_t* p_user_data)
{
    if (app_id == 0 || CTC_WB_SUBID(app_id) == SYS_WB_APPID_ACL_SUBID_PRESEL)
    {
        /*sync acl presel*/
        CTC_WB_INIT_DATA_T(p_wb_data, sys_wb_acl_presel_t, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL);
        p_user_data->data0 = (void*)p_wb_data;
        CTC_ERROR_RETURN(ctc_vector_traverse(SYS_ACL_PRESEL_VEC(lchip), _sys_tmm_acl_wb_sync_presel, (void*)p_user_data));

        if (p_wb_data->valid_cnt > 0)
        {
            CTC_ERROR_RETURN(ctc_wb_add_entry(p_wb_data));
        }
        CTC_WB_SYNC_END_DATA(CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL);
    }
    return CTC_E_NONE;
}

int32
sys_tmm_acl_wb_restore(uint8 lchip, ctc_wb_query_t* p_wb_query)
{
    int32  ret = CTC_E_NONE;
    uint32 entry_cnt = 0;
    sys_wb_acl_presel_t wb_acl_presel;
    sys_wb_acl_presel_t* p_wb_acl_presel = &wb_acl_presel;
    sys_acl_presel_t* p_acl_presel = NULL;

    /*restore acl presel*/
    sal_memset(&wb_acl_presel, 0, sizeof(sys_wb_acl_presel_t));
    CTC_WB_INIT_QUERY_T(p_wb_query, sys_wb_acl_presel_t, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL);
    CTC_WB_QUERY_ENTRY_BEGIN(p_wb_query);
        p_acl_presel = mem_malloc(MEM_ACL_MODULE, sizeof(sys_acl_presel_t));
        if (NULL == p_acl_presel)
        {
            return CTC_E_NO_MEMORY;
        }
        sal_memset(p_acl_presel, 0, sizeof(sys_acl_presel_t));
        sal_memcpy((uint8*)&wb_acl_presel, (uint8*)(p_wb_query->buffer) + entry_cnt * (p_wb_query->key_len + p_wb_query->data_len), p_wb_query->key_len + p_wb_query->data_len);
        entry_cnt++;

        /* restore lchip before mapping entry*/
        CTC_ERROR_RETURN(_sys_tmm_acl_wb_mapping_presel(lchip, p_wb_acl_presel, p_acl_presel, 0));
        ctc_vector_add(SYS_ACL_PRESEL_VEC(lchip), p_acl_presel->presel_id, (void*)p_acl_presel);
        if(p_acl_presel->fpae.flag == FPA_ENTRY_FLAG_UNINSTALLED)
        {
            sys_tmm_acl_destroy_presel(lchip, p_acl_presel->presel_id);
        }
    CTC_WB_QUERY_ENTRY_END(p_wb_query);
done:
    return ret;
}

#define _ACL_REORDER_
#define FPA_ENT_CNT(p_fpab, key_size) ((p_fpab)->sub_entry_count[key_size] - (p_fpab)->sub_free_count[key_size])
#if 0
STATIC int32
_sys_tmm_acl_reorder_entry_scatter(uint8 lchip, sys_acl_league_t* p_sys_league)
{
    uint16 start_offset_160 = 0;
    uint16 start_offset_320 = 0;
    uint16 start_offset_640 = 0;
    uint16 cur_width_160 = 0;
    uint16 cur_width_320 = 0;
    uint16 cur_width_640 = 0;
    int32 shift_160 = 0;
    int32 shift_320 = 0;
    int32 shift_640 = 0;
    int32 vary = 0;
    int32 vary_160 = 0;
    int32 vary_320 = 0;
    int32 vary_640 = 0;
    ctc_fpa_block_t tmp_fpab;
    ctc_fpa_block_info_t block_info;
    ctc_fpa_block_t* old_fpab = NULL;
    sys_acl_block_t * pb = NULL;
    uint8 same_size = 0;
    uint8 m_160 = 0;
    uint8 m_320 = 0;
    uint8 m_640 = 0;
    sys_acl_fpa_ecnt_t fpa_ecnt;

    SYS_ACL_DBG_FUNC();

    pb = &(SYS_ACL_BLOCK(lchip, p_sys_league->block_id));
    old_fpab = &pb->fpab;
    if (old_fpab->part)
    {
        return CTC_E_INVALID_CONFIG;
    }
    sal_memset(&tmp_fpab, 0, sizeof(ctc_fpa_block_t));
    sal_memcpy(&tmp_fpab, &pb->fpab, sizeof(ctc_fpa_block_t));

    same_size = pb->fpab.same_size;
    m_160 = same_size?1:2;
    m_320 = same_size?1:4;
    m_640 = same_size?1:8;

    if (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] != old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80])
    {
        old_fpab->start_offset[CTC_FPA_KEY_SIZE_160]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_160]  = 0;

        old_fpab->start_offset[CTC_FPA_KEY_SIZE_320]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_320]  = 0;

        old_fpab->start_offset[CTC_FPA_KEY_SIZE_640]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_640]  = 0;

        vary = (int32)old_fpab->entry_count - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80];
        CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_80, 0, vary, 0);
        fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] += vary;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80] += vary;
    }
    else
    {
        old_fpab->start_offset[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_80] = 0;

        start_offset_160 = 0;
        if ((DRV_IS_TMM(lchip) || DRV_IS_TMG(lchip)) && p_sys_league->block_id >= 16)
        {
            sys_usw_acl_split_fpa_block(lchip, old_fpab->entry_count, fpa_ecnt);
            start_offset_320 = fpa_ecnt[CTC_FPA_KEY_SIZE_160];
            start_offset_640 = start_offset_320 + fpa_ecnt[CTC_FPA_KEY_SIZE_320];
        }
        else
        {
            start_offset_320 = (old_fpab->entry_count)/2;
            start_offset_640 = start_offset_320 + (old_fpab->entry_count)/4;
        }

        cur_width_160 = (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160])*m_160;
        cur_width_320 = (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320])*m_320;
        cur_width_640 = (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640])*m_640;

        /*scatter default 160 size not enough, move 320 down*/
        if ((start_offset_320 - start_offset_160) < cur_width_160)
        {
            start_offset_320 = (start_offset_160 + cur_width_160);
            if (start_offset_320 % m_320)
            {
                old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += (m_320 - (start_offset_320 % m_320))/m_160;
                old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] += (m_320 - (start_offset_320 % m_320))/m_160;
                start_offset_320 += (m_320 - (start_offset_320 % m_320));
            }
        }
        /*scatter default 320 size not enough, move 640 down*/
        if ((start_offset_640 - start_offset_320) < cur_width_320)
        {
            start_offset_640 = (start_offset_320 + cur_width_320);
            if (start_offset_640 % m_640)
            {
                old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += (start_offset_320 - (start_offset_640 % m_640))/start_offset_320;
                old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += (start_offset_320 - (start_offset_640 % m_640))/start_offset_320;
                start_offset_640 += (m_640 - (start_offset_640 % m_640));
            }
        }
        /*scatter default 640 size not enough, move up*/
        if ((old_fpab->entry_count - start_offset_640) < cur_width_640)
        {
            start_offset_640 = (old_fpab->entry_count - cur_width_640);
        }
        /*scatter default 320 size not enough, move up*/
        if ((start_offset_640 - start_offset_320) < cur_width_320)
        {
            start_offset_320 = (start_offset_640 - cur_width_320);
        }

        vary_160 = (int32)(start_offset_320 - start_offset_160)/m_160 - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160];
        vary_320 = (int32)(start_offset_640 - start_offset_320)/m_320 - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320];
        vary_640 = (int32)(old_fpab->entry_count - start_offset_640)/m_640 - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640];

        shift_160 = (int32)start_offset_160 - (int32)old_fpab->start_offset[CTC_FPA_KEY_SIZE_160];
        shift_320 = (int32)start_offset_320 - (int32)old_fpab->start_offset[CTC_FPA_KEY_SIZE_320];
        shift_640 = (int32)start_offset_640 - (int32)old_fpab->start_offset[CTC_FPA_KEY_SIZE_640];

        if ((shift_320 < 0) && (shift_640 >= 0))
        {
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, shift_160, vary_160, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_160] = start_offset_160;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] += vary_160;
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_640, shift_640, vary_640, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_640] = start_offset_640;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] += vary_640;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640] += vary_640;
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_320, shift_320, vary_320, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_320] = start_offset_320;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += vary_320;
        }
        else if ((shift_320 < 0) && (shift_640 < 0))
        {
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, shift_160, vary_160, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_160] = start_offset_160;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] += vary_160;
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_320, shift_320, vary_320, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_320] = start_offset_320;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_640, shift_640, vary_640, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_640] = start_offset_640;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] += vary_640;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640] += vary_640;
        }
        else if ((shift_320 >= 0) && (shift_640 >= 0))
        {
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_640, shift_640, vary_640, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_640] = start_offset_640;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] += vary_640;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640] += vary_640;
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_320, shift_320, vary_320, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_320] = start_offset_320;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, shift_160, vary_160, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_160] = start_offset_160;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] += vary_160;
        }
        else if ((shift_320 >= 0) && (shift_640 < 0))
        {
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_320, shift_320, vary_320, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_320] = start_offset_320;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, shift_160, vary_160, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_160] = start_offset_160;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] += vary_160;
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_640, shift_640, vary_640, 0);
            fpa_usw_scatter(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
            old_fpab->start_offset[CTC_FPA_KEY_SIZE_640] = start_offset_640;
            old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] += vary_640;
            old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640] += vary_640;
        }
    }

    return CTC_E_NONE;
}
#endif

STATIC int32
_sys_tmm_acl_reorder_entry_down_to_up(uint8 lchip, sys_acl_league_t* p_sys_league)
{
    uint16 start_offset_160 = 0;
    uint16 start_offset_320 = 0;
    uint16 start_offset_640 = 0;
    int32 shift = 0;
    int32 vary = 0;
    int32 vary_160 = 0;
    int32 vary_320 = 0;
    int32 vary_640 = 0;
    int32 vblock_vary_160 = 0;
    int32 block2_vary_160 = 0;
    ctc_fpa_block_info_t block_info;
    ctc_fpa_block_t* old_fpab = NULL;
    ctc_fpa_block_t* tmp_fpab = NULL;
    sys_acl_block_t * pb = NULL;
    uint8 same_size = 0;
    uint8 m_160 = 0;
    uint8 m_320 = 0;
    uint8 m_640 = 0;
    uint8 part = 0;

    SYS_ACL_DBG_FUNC();

    pb = &(SYS_ACL_BLOCK(lchip, p_sys_league->block_id));
    old_fpab = &pb->fpab;
    if (old_fpab->part)
    {
        return CTC_E_INVALID_CONFIG;
    }

    same_size = pb->fpab.same_size;
    m_160 = same_size?1:2;
    m_320 = same_size?1:4;
    m_640 = same_size?1:8;

    if (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] != old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80])
    {
        old_fpab->start_offset[CTC_FPA_KEY_SIZE_160]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_160]  = 0;

        old_fpab->start_offset[CTC_FPA_KEY_SIZE_320]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_320]  = 0;

        old_fpab->start_offset[CTC_FPA_KEY_SIZE_640]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_640]  = 0;

        vary = (int32)old_fpab->entry_count - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80];
        CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_80, 0, vary, 0);
        fpa_usw_increase(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] += vary;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80] += vary;
    }
    else
    {
        old_fpab->start_offset[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_80] = 0;

        start_offset_640 = old_fpab->start_offset[CTC_FPA_KEY_SIZE_640] + (old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640]*m_640);
        vary_640 = (0 - (int32)old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640]);
        start_offset_320 = start_offset_640 - (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320])*m_320;
        vary_320 = (0 - (int32)old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320]);
        start_offset_160 = 0;
        vary_160 = ((int32)start_offset_320/m_160 - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160]);
        if (3 == old_fpab->part_num)
        {
            vblock_vary_160 = old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640]*3 + old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320]*2;
        }
        else if (2 == old_fpab->part_num)
        {
            vblock_vary_160 = old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640]*2 + old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320]*2;
        }
        SYS_ACL_PART_LOOP(part, old_fpab->part_num)
        {
            tmp_fpab = &pb[part].fpab;
            shift = (int32)start_offset_640 - (int32)old_fpab->start_offset[CTC_FPA_KEY_SIZE_640];
            if (part == 0)
            {
                CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_640, shift, vary_640, 0);
                fpa_usw_increase(p_usw_acl_master[lchip]->fpa, tmp_fpab, &block_info);
            }
            tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_640] = start_offset_640;
            tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] += vary_640;
            tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640] += vary_640;

            if (part == 2)
            {
                start_offset_320 = start_offset_640;
                vary_320 = 0;
                block2_vary_160 = ((int32)start_offset_320/m_160 - (int32)tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160]);
            }
            shift = (int32)start_offset_320 - (int32)tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_320];
            if (part == 0)
            {
                CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_320, shift, vary_320, 0);
                fpa_usw_increase(p_usw_acl_master[lchip]->fpa, tmp_fpab, &block_info);
            }
            tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_320] = start_offset_320;
            tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += vary_320;
        }
        if (!old_fpab->vblock)
        {
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, 0, vary_160, 0);
            fpa_usw_increase(p_usw_acl_master[lchip]->fpa, old_fpab, &block_info);
            tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_160] = start_offset_160;
            tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
            tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] += vary_160;
        }
        else
        {
            CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, 0, vblock_vary_160, old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160]+vary_160);
            fpa_usw_reorder_virtual(p_usw_acl_master[lchip]->fpa, old_fpab, 3, &block_info);
            SYS_ACL_PART_LOOP(part, old_fpab->part_num)
            {
                tmp_fpab = &pb[part].fpab;
                
                if (part == 2)
                {
                    tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += block2_vary_160;
                    tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160]  += block2_vary_160;

                }
                else
                {
                    tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
                    tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160]  += vary_160;                   
                }

            }
            old_fpab->vblock->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vblock_vary_160;
            old_fpab->vblock->sub_free_count[CTC_FPA_KEY_SIZE_160] += vblock_vary_160;
        }
    }

    return CTC_E_NONE;
}

STATIC int32
_sys_tmm_acl_reorder_entry_up_to_down(uint8 lchip, sys_acl_league_t* p_sys_league)
{
    uint16 start_offset_160 = 0;
    uint16 start_offset_320 = 0;
    uint16 start_offset_640 = 0;
    uint16 entry_count_160 = 0;
    int32 shift_160 = 0;
    int32 shift_320 = 0;
    int32 shift_640 = 0;
    int32 vary = 0;
    int32 vary_160 = 0;
    int32 vary_320 = 0;
    int32 vary_640 = 0;
    int32 vblock_vary = 0;
    ctc_fpa_block_info_t block_info;
    ctc_fpa_block_t* old_fpab = NULL;
    ctc_fpa_block_t* tmp_fpab = NULL;
    sys_acl_block_t * pb = NULL;
    uint8 same_size = 0;
    uint8 part = 0;

    SYS_ACL_DBG_FUNC();

    pb = &(SYS_ACL_BLOCK(lchip, p_sys_league->block_id));
    old_fpab = &pb->fpab;
    if (old_fpab->part)
    {
        return CTC_E_INVALID_CONFIG;
    }

    same_size = pb->fpab.same_size;

    if (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] != old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80])
    {
        old_fpab->start_offset[CTC_FPA_KEY_SIZE_160]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_160]  = 0;

        old_fpab->start_offset[CTC_FPA_KEY_SIZE_320]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_320]  = 0;

        old_fpab->start_offset[CTC_FPA_KEY_SIZE_640]    = old_fpab->entry_count;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640]  = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_640]  = 0;

        vary = (int32)old_fpab->entry_count - (int32)old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80];
        CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_80, 0, vary, 0);
        fpa_usw_decrease(p_usw_acl_master[lchip]->fpa, &pb->fpab, &block_info);
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] += vary;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80] += vary;
    }
    else
    {
        old_fpab->start_offset[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_80] = 0;
        old_fpab->sub_rsv_count[CTC_FPA_KEY_SIZE_80] = 0;

        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "%6s %6d %6d %6d/%6s %6d %6d %6d/%6s %6d %6d %6d\n", "START",160,320,640,"SHIFT",160,320,640,"VARY",160,320,640);
        start_offset_160 = 0;
        if (same_size && old_fpab->part_num == 3 && FPA_ENT_CNT(old_fpab->vblock, CTC_FPA_KEY_SIZE_160) > FPA_ENT_CNT(old_fpab, CTC_FPA_KEY_SIZE_320))
        {
            start_offset_320 = (FPA_ENT_CNT(old_fpab->vblock, CTC_FPA_KEY_SIZE_160) - FPA_ENT_CNT(old_fpab, CTC_FPA_KEY_SIZE_320) + 2) / 3;
            vblock_vary = start_offset_320 * 3 + FPA_ENT_CNT(old_fpab, CTC_FPA_KEY_SIZE_320) - old_fpab->vblock->sub_entry_count[CTC_FPA_KEY_SIZE_160];
            start_offset_320 += start_offset_160;
        }
        else if (same_size && old_fpab->part_num == 3)
        {
            start_offset_320 = start_offset_160;
            vblock_vary = FPA_ENT_CNT(old_fpab, CTC_FPA_KEY_SIZE_320) - old_fpab->vblock->sub_entry_count[CTC_FPA_KEY_SIZE_160];
        }
        else if (same_size && old_fpab->part_num == 2)
        {
            start_offset_320 = (FPA_ENT_CNT(old_fpab->vblock, CTC_FPA_KEY_SIZE_160) + 1) / 2;
            vblock_vary = start_offset_320 * 2 - old_fpab->vblock->sub_entry_count[CTC_FPA_KEY_SIZE_160];
        }
        else
        {
            start_offset_320 = start_offset_160 + (old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160]);
        }
        SYS_ACL_PART_LOOP(part, old_fpab->part_num)
        {
            tmp_fpab = &pb[part].fpab;

            if (same_size && part == 2)
            {
                start_offset_320 += old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320];
            }
            start_offset_640 = start_offset_320 + (tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] - tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320]);

            vary_160 = (int32)(start_offset_320 - start_offset_160) - (int32)tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160];
            vary_320 = (int32)(start_offset_640 - start_offset_320) - (int32)tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320];
            vary_640 = (int32)(tmp_fpab->entry_count - start_offset_640) - (int32)tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640];

            shift_160 = (int32)start_offset_160 - (int32)tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_160];
            shift_320 = (int32)start_offset_320 - (int32)tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_320];
            shift_640 = (int32)start_offset_640 - (int32)tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_640];

            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "%6s %6d %6d %6d/%6s %6d %6d %6d/%6s %6d %6d %6d\n",
                                                    "",start_offset_160,start_offset_320,start_offset_640,
                                                    "",shift_160,shift_320,shift_640,
                                                    "",vary_160,vary_320,vary_640);
            if (start_offset_320 <= old_fpab->start_offset[CTC_FPA_KEY_SIZE_320])
            {
                /*adjust vblock 160*/
                if (old_fpab->vblock)
                {
                    if(part == 0)
                    {
                        CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, shift_160, vblock_vary, start_offset_320);
                        block_info.key160_count_valid = 1;
                        fpa_usw_reorder_virtual(p_usw_acl_master[lchip]->fpa, old_fpab, 2, &block_info);
                        block_info.key160_count_valid = 0;
                        old_fpab->vblock->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vblock_vary;
                        old_fpab->vblock->sub_free_count[CTC_FPA_KEY_SIZE_160] += vblock_vary;
                        entry_count_160 = old_fpab->vblock->sub_entry_count[CTC_FPA_KEY_SIZE_160] - old_fpab->vblock->sub_free_count[CTC_FPA_KEY_SIZE_160];
                	}
                }
                else
                {
                    CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, shift_160, vary_160, 0);
                    fpa_usw_decrease(p_usw_acl_master[lchip]->fpa, old_fpab, &block_info);
                    entry_count_160 = old_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] - old_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160];
                }
                tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_160] = start_offset_160;
                tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
                tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] = entry_count_160 > tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] ? 0 : tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] - entry_count_160;
                entry_count_160 = entry_count_160 > tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] ? entry_count_160 - tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] : 0;
                /*adjust 320*/
                if (part == 0)
                {
                    CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_320, shift_320, vary_320, 0);
                    fpa_usw_decrease(p_usw_acl_master[lchip]->fpa, tmp_fpab, &block_info);
                }
                tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_320] = start_offset_320;
                tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += vary_320;
                tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += vary_320;
            }
            else    /*only vblock and part_num = 3 happend*/
            {
                /*adjust 320*/
                if (part == 0)
                {
                    CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_320, 0, 0, 0);
                    fpa_usw_increase(p_usw_acl_master[lchip]->fpa, tmp_fpab, &block_info);
                    CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_320, shift_320, vary_320, 0);
                    fpa_usw_decrease(p_usw_acl_master[lchip]->fpa, tmp_fpab, &block_info);
                }
                tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_320] = start_offset_320;
                tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_320] += vary_320;
                tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_320] += vary_320;
                /*adjust vblock 160*/
                if(part == 0)
                {
                    CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_160, shift_160, vblock_vary, start_offset_320);
                    block_info.key160_count_valid = 1;
                    fpa_usw_reorder_virtual(p_usw_acl_master[lchip]->fpa, old_fpab, 2, &block_info);
                    block_info.key160_count_valid = 0;
                    old_fpab->vblock->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vblock_vary;
                    old_fpab->vblock->sub_free_count[CTC_FPA_KEY_SIZE_160] += vblock_vary;
                    entry_count_160 = old_fpab->vblock->sub_entry_count[CTC_FPA_KEY_SIZE_160] - old_fpab->vblock->sub_free_count[CTC_FPA_KEY_SIZE_160];
            	}
                tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_160] = start_offset_160;
                tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] += vary_160;
                tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_160] = entry_count_160 > tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] ? 0 : tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] - entry_count_160;
                entry_count_160 = entry_count_160 > tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] ? entry_count_160 - tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_160] : 0;
            }
            if (part == 0)
            {
                CTC_FPA_BUILD_BLOCK_INFO(block_info, CTC_FPA_KEY_SIZE_640, shift_640, vary_640, 0);
                fpa_usw_decrease(p_usw_acl_master[lchip]->fpa, tmp_fpab, &block_info);
            }
            tmp_fpab->start_offset[CTC_FPA_KEY_SIZE_640] = start_offset_640;
            tmp_fpab->sub_entry_count[CTC_FPA_KEY_SIZE_640] += vary_640;
            tmp_fpab->sub_free_count[CTC_FPA_KEY_SIZE_640] += vary_640;
        }

        if(pb->block_status == SYS_ACL_BLOCK_STATUS_UNMERGE)
        {
            uint8 index = 0;
            uint8 old_part_num  = pb->fpab.part_num;
            uint8 dir           = SYS_ACL_BLOCK_DIR(pb->block_id);
            uint8 global_pp_en = SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, dir, SYS_ACL_BLOCK_PRIO(pb->block_id));
            sys_acl_lt_block_t* p_ltb = &p_usw_acl_master[lchip]->lt_block[p_sys_league->block_id];
            
            sys_tmm_acl_compress_link_block(lchip, pb, NULL);
            
            for (index = 0; index <SYS_ACL_PRESEL_TCAM_DEPTH; index++)
            {
                SYS_ACL_IF_CONTINUE(!CTC_IS_BIT_SET(p_ltb->is_key160, index));
                for (part=pb->fpab.part_num; part<old_part_num; part++)
                {
                    sys_tmm_acl_op_presel_hw(lchip, p_sys_league->block_id+part, SYS_ACL_PRESEL_OP_MODE_DEL, index, NULL, 0, global_pp_en);
                }
            }
        }
    }

    return CTC_E_NONE;
}
int32
sys_tmm_acl_reorder_entry(uint8 lchip, ctc_acl_reorder_t* reorder)
{
    int32 ret = 0;
    uint8 part = 0;
    uint8 block_id = 0;
    uint8 have_entry = 0;
    sys_acl_block_t*  p_acb = NULL;
    sys_acl_league_t* p_sys_league = NULL;

    SYS_ACL_DBG_FUNC();
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM,"sys_usw_acl_reorder_entry,mode:%u,dir:%u,prio:%d \n", reorder->mode,reorder->dir,reorder->acl_priority);
    SYS_USW_ACL_LOCK(lchip);

    block_id = (CTC_INGRESS == reorder->dir ? reorder->acl_priority: reorder->acl_priority + ACL_IGS_BLOCK_MAX_NUM);
    p_sys_league = &(p_usw_acl_master[lchip]->league[block_id]);
    p_acb = &p_usw_acl_master[lchip]->block[block_id];

    /*acl priority'lookup level is empty*/
    if (!p_sys_league->lkup_level_bitmap)
    {
        SYS_USW_ACL_RETURN_UNLOCK(CTC_E_NONE);
    }

    /*no entry*/
    SYS_ACL_PART_LOOP(part, p_acb->fpab.part_num)
    {
        if (p_acb[part].fpab.entry_count != p_acb[part].fpab.free_count)
        {
            have_entry = 1;
            break;
        }
    }
    if (0 == have_entry)
    {
        SYS_USW_ACL_RETURN_UNLOCK(CTC_E_NONE);
    }

    switch (reorder->mode)
    {
#if 0        
        case CTC_ACL_REORDER_MODE_SCATTER:
        {
            ret = _sys_tmm_acl_reorder_entry_scatter(lchip, p_sys_league);
            break;
        }
#endif        
        case CTC_ACL_REORDER_MODE_DOWNTOUP:
        {
            ret = _sys_tmm_acl_reorder_entry_down_to_up(lchip, p_sys_league);
            break;
        }
        case CTC_ACL_REORDER_MODE_UPTODOWN:
        {
            ret = _sys_tmm_acl_reorder_entry_up_to_down(lchip, p_sys_league);
            break;
        }
        default :
        {
            SYS_USW_ACL_RETURN_UNLOCK(CTC_E_INVALID_PARAM);
        }
    }

    SYS_USW_ACL_UNLOCK(lchip);
    return ret;
}

int32
sys_at_acl_add_hash_nsh_sel_field(uint8 lchip, uint8 field_sel_id, ctc_field_key_t* sel_field, uint8 is_add)
{
    uint32 cmd  = 0;
    uint32 value = 0;
    FlowNshHashFieldSelect_m   ds_sel;
    sal_memset(&ds_sel, 0, sizeof(FlowNshHashFieldSelect_m));

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_PARAM," %% PARAM: field_sel_id %u -- key_field_type %u\n", field_sel_id, sel_field->type);

    if(is_add)
    {
        value = 1;
    }

    cmd = DRV_IOR(FlowNshHashFieldSelect_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, field_sel_id, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &ds_sel));

    switch(sel_field->type)
    {
    case CTC_FIELD_KEY_PORT:
        if(is_add)
        {
            ctc_field_port_t* pport_sel = (ctc_field_port_t*)sel_field->ext_data;
            CTC_PTR_VALID_CHECK(sel_field->ext_data);
            if(CTC_FIELD_PORT_TYPE_GPORT == pport_sel->type)
            {
                SetFlowNshHashFieldSelect(V, globalPortType_f, &ds_sel, ACL_HASH_GPORT_TYPE_GPORT);
            }
            if(CTC_FIELD_PORT_TYPE_LOGIC_PORT == pport_sel->type)
            {
                SetFlowNshHashFieldSelect(V, globalPortType_f, &ds_sel, ACL_HASH_GPORT_TYPE_LOGIC_PORT);
            }
        }
        else
        {
            SetFlowNshHashFieldSelect(V, globalPortType_f, &ds_sel, ACL_HASH_GPORT_TYPE_NONE);
        }
        break;
    case CTC_FIELD_KEY_METADATA:
        SetFlowNshHashFieldSelect(V, globalPortType_f, &ds_sel, is_add ? ACL_HASH_GPORT_TYPE_METADATA : 0);
        break;

    case CTC_FIELD_KEY_NSH_SPI:
        SetFlowNshHashFieldSelect(V, nshSphSpiEn_f, &ds_sel, value);
        break;

    case CTC_FIELD_KEY_NSH_SI:
        SetFlowNshHashFieldSelect(V, nshSphSiEn_f, &ds_sel, value);
        break;
    default:
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, " Feature not supported \n");
        return CTC_E_NOT_SUPPORT;

    }

    SetFlowNshHashFieldSelect(V, nshValidEn_f, &ds_sel, 1);
    cmd = DRV_IOW(FlowNshHashFieldSelect_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, field_sel_id, DRV_CMD_BMP_EN(cmd, MCHIP_FEATURE_PP_EN(lchip,CTC_FEATURE_ACL)), &ds_sel));

    return CTC_E_NONE;
}

int32
sys_at_acl_add_hash_nsh_key_field(uint8 lchip, ctc_field_key_t* key_field, sys_acl_hash_entry_t* pe, uint8 is_add)
{
    int32           ret         = CTC_E_NONE;
    uint32          data        = 0;
    uint32          tmp_data    = 0;
    sys_acl_buffer_t* p_buffer  = NULL;
    FlowNshHashFieldSelect_m ds_sel;
    ctc_field_port_t* pport= (ctc_field_port_t*)key_field->ext_data;

    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);

    SYS_USW_ACL_HASH_VALID_CHECK(pe->hash_valid);

    if(is_add)
    {
        data = key_field->data;
    }

    p_buffer = &pe->ua->buffer;

    switch(key_field->type)
    {
    case CTC_FIELD_KEY_NSH_SPI:
        if(CTC_BMP_ISSET(&p_buffer->action[ACL_HASH_SEL_OFFSET], FlowNshHashFieldSelect_nshSphSpiEn_f))
        {
            CTC_MAX_VALUE_CHECK(data, 0xFFFFFF);
            SetDsFlowNshForwardHashKey(V, nshSphSpi_f, p_buffer->key, data);
            CTC_BMP_UNSET(&p_buffer->action[ACL_HASH_SEL_OFFSET],FlowNshHashFieldSelect_nshSphSpiEn_f);
        }
        break;
    case CTC_FIELD_KEY_NSH_SI:
        if(CTC_BMP_ISSET(&p_buffer->action[ACL_HASH_SEL_OFFSET], FlowNshHashFieldSelect_nshSphSiEn_f))
        {
            CTC_MAX_VALUE_CHECK(data, 0xFF);
            SetDsFlowNshForwardHashKey(V, nshSphSi_f, p_buffer->key, data);
            CTC_BMP_UNSET(&p_buffer->action[ACL_HASH_SEL_OFFSET],FlowNshHashFieldSelect_nshSphSiEn_f);
        }
        break;
    case CTC_FIELD_KEY_PORT:
        CTC_PTR_VALID_CHECK(key_field->ext_data);
        CTC_ERROR_RETURN(_sys_usw_acl_get_field_sel(lchip, CTC_ACL_KEY_HASH_NSH, pe->hash_field_sel_id, &ds_sel));
        if(CTC_FIELD_PORT_TYPE_GPORT == pport->type)
        {
            if(ACL_HASH_GPORT_TYPE_GPORT == GetFlowNshHashFieldSelect(V, globalPortType_f, &ds_sel))
            {
                SYS_GLOBAL_PORT_CHECK(pport->gport);
                tmp_data = SYS_MAP_CTC_GPORT_TO_DRV_GPORT(pport->gport);
                SetDsFlowNshForwardHashKey(V, globalSrcPort_f, p_buffer->key, tmp_data);
                CTC_BMP_UNSET(&p_buffer->action[ACL_HASH_SEL_OFFSET],FlowNshHashFieldSelect_globalPortType_f);
            }
        }
        if(CTC_FIELD_PORT_TYPE_LOGIC_PORT == pport->type)
        {
            if(ACL_HASH_GPORT_TYPE_LOGIC_PORT == GetFlowNshHashFieldSelect(V, globalPortType_f, &ds_sel))
            {
                CTC_MAX_VALUE_CHECK(pport->logic_port, 0xFFFF);
                SetDsFlowNshForwardHashKey(V, globalSrcPort_f, p_buffer->key, pport->logic_port);
                CTC_BMP_UNSET(&p_buffer->action[ACL_HASH_SEL_OFFSET],FlowNshHashFieldSelect_globalPortType_f);
            }
        }
        break;
    case CTC_FIELD_KEY_METADATA:
        CTC_ERROR_RETURN(_sys_usw_acl_get_field_sel(lchip, CTC_ACL_KEY_HASH_NSH, pe->hash_field_sel_id, &ds_sel));
        if(ACL_HASH_GPORT_TYPE_METADATA == GetFlowNshHashFieldSelect(V, globalPortType_f, &ds_sel))
        {
            CTC_MAX_VALUE_CHECK(data, (1<<DRV_ENUM(DRV_KEY_METADATA_BITS))-1);
            SetDsFlowNshForwardHashKey(V, globalSrcPort_f, p_buffer->key, data|(1<<DRV_ENUM(DRV_KEY_METADATA_BITS)));
            CTC_BMP_UNSET(&p_buffer->action[ACL_HASH_SEL_OFFSET],FlowNshHashFieldSelect_globalPortType_f);
        }
        break;
    case SYS_FIELD_KEY_HASH_SEL_ID:
        CTC_MAX_VALUE_CHECK(data, 0xF);
        SetDsFlowNshForwardHashKey(V, flowFieldSel_f, p_buffer->key, data);
        break;

    default:
        ret = CTC_E_NOT_SUPPORT;
        break;
    }
    return ret;
}

#define _FLEX_HASH_

#if defined(ARCTIC)
int32
sys_at_acl_add_flex_hash_field(uint8 lchip, ctc_field_key_t* pKey, void* pe, uint8 is_add)
{
    sys_xdata_fk_io_t xdata_fk;
    ctc_acl_udf_t* p_udf_entry = NULL;
    sys_acl_udf_entry_t* p_sys_udf_entry = NULL;
    sys_acl_flex_hash_t* fk = ACL_SHARE_PTR(((sys_acl_entry_t*)pe)->ua->buffer.fk, sys_acl_flex_hash_t);
    uint32 value = 0;
    uint32 hw_udf[4] = {0};
    uint8  granularity = 1;
    uint32 key_id = 0;
    uint8 discard_type = 0;
    uint8 i = 0;
    uint8 step = 0;
    uint8 num = 0;
    uint8 key_type = 0;
    uint16 fk_field[CTC_ACL_UDF_FIELD_NUM] = {SYS_FK_F_UDF_DATA0,SYS_FK_F_UDF_DATA1,SYS_FK_F_UDF_DATA2,SYS_FK_F_UDF_DATA3,
                                              SYS_FK_F_UDF_DATA4,SYS_FK_F_UDF_DATA5,SYS_FK_F_UDF_DATA6,SYS_FK_F_UDF_DATA7};

    CTC_PTR_VALID_CHECK(pKey);
    if(0 == ((sys_acl_entry_t*)pe)->group->group_info.block_id)
    {
        ((sys_acl_entry_t*)pe)->ua->buffer.fk = NULL;
    }
    key_type = ((sys_acl_entry_t*)pe)->key_type;
    ((sys_acl_entry_t*)pe)->key_type = fk->origin_key_type;
    _sys_usw_acl_get_table_id(lchip, (sys_acl_entry_t*)pe, &key_id, NULL, NULL);
    ((sys_acl_entry_t*)pe)->ua->buffer.fk = (sys_acl_flex_key_t*)fk;
    ((sys_acl_entry_t*)pe)->key_type = key_type;
    xdata_fk.type = ((sys_acl_entry_t*)pe)->group->group_info.block_id > 0 ? CTC_XDATA_LOCAL_TYPE_FLOW_HASH_EXT : CTC_XDATA_LOCAL_TYPE_FLOW_HASH;
    xdata_fk.tbl_id = key_id;
    xdata_fk.hash_type = fk->hash_type;
    switch(pKey->type)
    {
    case CTC_FIELD_KEY_UDF:
        if(is_add)
        {
            CTC_PTR_VALID_CHECK(pKey->ext_data);
            p_udf_entry = pKey->ext_data;
            _sys_usw_acl_get_udf_info(lchip, p_udf_entry->udf_id, &p_sys_udf_entry);
            if (!p_sys_udf_entry || !p_sys_udf_entry->key_index_used)
            {
                return CTC_E_NOT_EXIST;
            }
            granularity = p_sys_udf_entry->granularity;
            SYS_USW_SET_HW_UDF(granularity, hw_udf, p_udf_entry->udf);
        }
        xdata_fk.fk_field = SYS_FK_F_UDF_HIT_INDEX;
        value = is_add ? p_sys_udf_entry->udf_hit_index : 0;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, value));

        for(i = 0; i< CTC_ACL_UDF_FIELD_NUM;i ++)
        {
            if(!CTC_IS_BIT_SET(fk->udf_bmp, i))
            {
                continue;
            }
            xdata_fk.fk_field = fk_field[i];
            step = (i%2) ? 16: 0;
            value = is_add ? ((hw_udf[i/2] >>step) & 0xFFFF) : 0;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, value));
        }
        break;
    case CTC_FIELD_KEY_L3_TYPE:
        CTC_MAX_VALUE_CHECK(pKey->data, 0xF);
        xdata_fk.fk_field = SYS_FK_F_PR_L3_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_L4_TYPE:
        CTC_MAX_VALUE_CHECK(pKey->data, CTC_CONST16-1);
        xdata_fk.fk_field = SYS_FK_F_PR_L4_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_L4_USER_TYPE:
        CTC_MAX_VALUE_CHECK(pKey->data, 0xB);
        xdata_fk.fk_field = SYS_FK_F_PR_L4_USER_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_VLAN_NUM:
        CTC_MAX_VALUE_CHECK(pKey->data, 0x7);
        xdata_fk.fk_field = SYS_FK_F_PR_VLAN_NUM;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_IP_FRAG:
        CTC_MAX_VALUE_CHECK(pKey->data, CTC_IP_FRAG_MAX-1);
        xdata_fk.fk_field = SYS_FK_F_PR_FRAG_INFO;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_DECAP:
        CTC_MAX_VALUE_CHECK(pKey->data, 1);
        xdata_fk.fk_field = SYS_FK_F_IS_DECAP;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_IP_HDR_ERROR:
        CTC_MAX_VALUE_CHECK(pKey->data, 0x1);
        xdata_fk.fk_field = SYS_FK_F_PR_IP_HDR_ERROR;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_DISCARD:
        if(is_add)
        {
            CTC_PTR_VALID_CHECK(pKey->ext_data);
            discard_type = *((uint32*)(pKey->ext_data));
            CTC_ERROR_RETURN(_sys_usw_acl_check_discard_type(discard_type));
        }
        xdata_fk.fk_field = SYS_FK_F_DISCARD;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? 1 : 0));
        xdata_fk.fk_field = SYS_FK_F_DISCARD_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? discard_type : 0));
        break;
    case CTC_FIELD_KEY_L2_TYPE:
        CTC_MAX_VALUE_CHECK(pKey->data, 0x7);
        xdata_fk.fk_field = SYS_FK_F_PR_L2_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_STP_STATE:
        CTC_MAX_VALUE_CHECK(pKey->data, 3);
        xdata_fk.fk_field = SYS_FK_F_STP_STATE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_CTAG_VALID:
        xdata_fk.fk_field = SYS_FK_F_CVLAN_ID_VALID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? 1 : 0));
        break;
    case CTC_FIELD_KEY_STAG_VALID:
        xdata_fk.fk_field = SYS_FK_F_SVLAN_ID_VALID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? 1 : 0));
        break;
    case CTC_FIELD_KEY_SVLAN_ID:
        SYS_ACL_VLAN_ID_CHECK(pKey->data);
        xdata_fk.fk_field = SYS_FK_F_SVLAN_ID_LO;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? (pKey->data & 0xff):0));
        xdata_fk.fk_field = SYS_FK_F_SVLAN_ID_HI;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? (pKey->data >> 8):0));
        break;
    case CTC_FIELD_KEY_CVLAN_ID:
        SYS_ACL_VLAN_ID_CHECK(pKey->data);
        xdata_fk.fk_field = SYS_FK_F_CVLAN_ID_LO;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? (pKey->data & 0xff):0));
        xdata_fk.fk_field = SYS_FK_F_CVLAN_ID_HI;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? (pKey->data >> 8):0));
        break;
    case CTC_FIELD_KEY_INTERFACE_ID:
        CTC_MAX_VALUE_CHECK(pKey->data, (MCHIP_CAP(SYS_CAP_SPEC_L3IF_NUM)-1))
        xdata_fk.fk_field = SYS_FK_F_L3_IF_ID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_LOGIC_SRC_PORT:
        CTC_MAX_VALUE_CHECK(pKey->data, 0xFFFF);
        xdata_fk.fk_field = SYS_FK_F_LOGIC_SRC_PORT;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_FID:
        CTC_MAX_VALUE_CHECK(pKey->data, 0x7FFF);
        xdata_fk.fk_field = SYS_FK_F_FID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_VRFID:
        CTC_MAX_VALUE_CHECK(pKey->data, MCHIP_CAP(SYS_CAP_SPEC_MAX_VRFID));
        xdata_fk.fk_field = SYS_FK_F_VRF_ID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? pKey->data : 0));
        break;
    case CTC_FIELD_KEY_XDATA:
        if(is_add)
        {
            CTC_PTR_VALID_CHECK(pKey->ext_data);
            xdata_fk.xdata = pKey->ext_data;
            num = xdata_fk.xdata->num;
        }
        else
        {
            num = 1;
        }

        xdata_fk.fk_field = SYS_FK_F_XDATA;
        for (i = 0; i < num; i++)
        {
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? 1 : 0));
        }
        break;
    case CTC_FIELD_KEY_PORT_CID:
        {
            ctc_xdata_t xdata;
            ctc_xdata_field_t field;
            sal_memset(&field,0, sizeof(ctc_xdata_field_t));
            xdata.fields = &field;
            xdata.num = 1;
            xdata.type = CTC_XDATA_LOCAL_TYPE_FLOW_HASH_EXT;
            xdata.prof_id = 0;
            field.width = CTC_XDATA_WIDTH_4;
            field.data = is_add ?pKey->data>>12:0;
            field.offset = SYS_XDATA_PORT_CID_POS;
            xdata_fk.fk_field = SYS_FK_F_XDATA;
            xdata_fk.xdata  = &xdata;
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->set_fk_field(lchip, &xdata_fk, ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.key, is_add ? 1 : 0));
        }
        break;

    default:
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_SUPPORT, "not support field!");
    }
    CTC_BMP_UNSET(ACL_SHARE_PTR(pe,sys_acl_entry_t)->ua->buffer.kset.w, pKey->type);
    return CTC_E_NONE;
}

int32
sys_at_acl_get_flex_hash_field(uint8 lchip, ctc_field_key_t* pKey, sys_acl_hash_entry_t* pe)
{
    sys_xdata_fk_io_t xdata_fk;
    sys_acl_udf_entry_t* p_udf_entry = NULL;
    sys_acl_flex_hash_t* fk = ACL_SHARE_PTR(((sys_acl_entry_t*)pe)->ua->buffer.fk, sys_acl_flex_hash_t);
    uint32 value = 0;
    uint32 granularity = 0;
    uint32 hw_udf[4] = {0};
    uint32 key_id = 0;
    uint8 i = 0;
    uint8 step = 0;
    uint8 key_type = 0;
    uint16 fk_field[CTC_ACL_UDF_FIELD_NUM] = {SYS_FK_F_UDF_DATA0,SYS_FK_F_UDF_DATA1,SYS_FK_F_UDF_DATA2,SYS_FK_F_UDF_DATA3,
                                              SYS_FK_F_UDF_DATA4,SYS_FK_F_UDF_DATA5,SYS_FK_F_UDF_DATA6,SYS_FK_F_UDF_DATA7};
    CTC_PTR_VALID_CHECK(pKey);

    if(0 == pe->group->group_info.block_id)
    {
        ((sys_acl_entry_t*)pe)->ua->buffer.fk = NULL;
    }
    key_type = ((sys_acl_entry_t*)pe)->key_type;
    ((sys_acl_entry_t*)pe)->key_type = fk->origin_key_type;
    _sys_usw_acl_get_table_id(lchip, (sys_acl_entry_t*)pe, &key_id, NULL, NULL);
    ((sys_acl_entry_t*)pe)->ua->buffer.fk = (sys_acl_flex_key_t*)fk;
    ((sys_acl_entry_t*)pe)->key_type = key_type;

    xdata_fk.type = (pe->group->group_info.block_id > 0)?CTC_XDATA_LOCAL_TYPE_FLOW_HASH_EXT : CTC_XDATA_LOCAL_TYPE_FLOW_HASH;
    xdata_fk.tbl_id = key_id;
    xdata_fk.hash_type = fk->hash_type;
    switch(pKey->type)
    {
    case CTC_FIELD_KEY_UDF:
        CTC_PTR_VALID_CHECK(pKey->ext_data);

        _sys_usw_acl_get_udf_entry_by_ad_id(lchip, ACL_SHARE_PTR(pe->ua->buffer.fk,sys_acl_flex_hash_t)->hash_type, &p_udf_entry);
        granularity = p_udf_entry ? p_udf_entry->granularity : 1;

        for(i = 0; i< CTC_ACL_UDF_FIELD_NUM;i ++)
        {
            if(!CTC_IS_BIT_SET(fk->udf_bmp, i))
            {
                continue;
            }
            xdata_fk.fk_field = fk_field[i];
            CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
            step = (i%2) ? 16: 0;
            hw_udf[i/2] |= (value << step);
        }

        SYS_USW_SET_USR_UDF(granularity, (uint8*)(pKey->ext_data), hw_udf);
        break;
    case CTC_FIELD_KEY_L3_TYPE:
        xdata_fk.fk_field = SYS_FK_F_PR_L3_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_L4_TYPE:
        xdata_fk.fk_field = SYS_FK_F_PR_L4_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_L4_USER_TYPE:
        xdata_fk.fk_field = SYS_FK_F_PR_L4_USER_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;

        break;
    case CTC_FIELD_KEY_VLAN_NUM:
        xdata_fk.fk_field = SYS_FK_F_PR_VLAN_NUM;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;

        break;
    case CTC_FIELD_KEY_IP_FRAG:
        xdata_fk.fk_field = SYS_FK_F_PR_FRAG_INFO;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_DECAP:
        xdata_fk.fk_field = CTC_FIELD_KEY_DECAP;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_IP_HDR_ERROR:
        xdata_fk.fk_field = SYS_FK_F_PR_IP_HDR_ERROR;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_DISCARD:
        xdata_fk.fk_field = SYS_FK_F_DISCARD;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        xdata_fk.fk_field = SYS_FK_F_DISCARD_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        *(uint32*)pKey->ext_data = value;
        break;
    case CTC_FIELD_KEY_L2_TYPE:
        xdata_fk.fk_field = SYS_FK_F_PR_L2_TYPE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;

        break;
    case CTC_FIELD_KEY_STP_STATE:
        xdata_fk.fk_field = SYS_FK_F_STP_STATE;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_CTAG_VALID:
        xdata_fk.fk_field = SYS_FK_F_CVLAN_ID_VALID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_STAG_VALID:
        xdata_fk.fk_field = SYS_FK_F_SVLAN_ID_VALID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_SVLAN_ID:
        xdata_fk.fk_field = SYS_FK_F_SVLAN_ID_LO;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        xdata_fk.fk_field = SYS_FK_F_SVLAN_ID_HI;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data |= (value << 8);
        break;
    case CTC_FIELD_KEY_CVLAN_ID:
        xdata_fk.fk_field = SYS_FK_F_CVLAN_ID_LO;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        xdata_fk.fk_field = SYS_FK_F_CVLAN_ID_HI;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data |= (value << 8);
        break;
    case CTC_FIELD_KEY_INTERFACE_ID:
        xdata_fk.fk_field = CTC_FIELD_KEY_INTERFACE_ID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_LOGIC_SRC_PORT:
        xdata_fk.fk_field = SYS_FK_F_LOGIC_SRC_PORT;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_FID:
        xdata_fk.fk_field = SYS_FK_F_FID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_VRFID:
        xdata_fk.fk_field = SYS_FK_F_VRF_ID;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));
        pKey->data = value;
        break;
    case CTC_FIELD_KEY_PORT_CID:
    case CTC_FIELD_KEY_XDATA:
        xdata_fk.fk_field = SYS_FK_F_XDATA;
        xdata_fk.xdata = (ctc_xdata_t*)pKey->ext_data;
        CTC_ERROR_RETURN(MCHIP_XDATA(lchip)->get_fk_field(lchip, &xdata_fk, pe->ua->buffer.key, &value));

        break;
    default:
        SYS_ACL_DUMP_ERROR_RETURN(CTC_E_NOT_SUPPORT, "not support field!");
    }
    return CTC_E_NONE;
}

int32
sys_at_acl_show_flex_hash_field(uint8 lchip, sys_acl_hash_entry_t* pe)
{
    sys_acl_flex_hash_t* fk = (sys_acl_flex_hash_t*)pe->ua->buffer.fk;
    ctc_field_key_t key;
    uint32 value[4] = {0};
    uint16  qid = 0;

    CTC_BMP__ITER(fk->kset.w, CTC_FIELD_KEY_NUM, qid)
    {
        switch (qid)
        {
        case CTC_FIELD_KEY_UDF:
        {
            sal_memset(&key, 0, sizeof(key));
            key.type = qid;
            key.ext_data = value;
            sal_memset(value, 0, sizeof(value));
            sys_at_acl_get_flex_hash_field(lchip, &key, pe);
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %s\n", ifp_field_str[qid], sys_usw_acl_output_udf((uint8*)&value[0]));
            break;
        }
        case CTC_FIELD_KEY_DISCARD:
        {
            sal_memset(&key, 0, sizeof(key));
            key.type = qid;
            key.ext_data = value;
            sal_memset(value, 0, sizeof(value));
            sys_at_acl_get_flex_hash_field(lchip, &key, pe);
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %-6u Type %-6u\n", ifp_field_str[qid], key.data, *((uint32*)key.ext_data));

            break;
        }
        case CTC_FIELD_KEY_XDATA:
        {
            ctc_xdata_t xdata;
            ctc_xdata_field_t fields[32];
            uint8 loop = 0;

            sal_memset(&key, 0, sizeof(key));
            sal_memset(&xdata,0, sizeof(ctc_xdata_t));
            sal_memset(fields,0, 32*sizeof(ctc_xdata_field_t));
            xdata.fields = fields;

            key.type = qid;
            key.ext_data = &xdata;
            sys_at_acl_get_flex_hash_field(lchip, &key, pe);
            if(xdata.num)
            {
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "  %-16s: %-6s %-6s %-8s\n", "XDATA", "WIDTH", "OFFSET","DATA");
                for (loop = 0; loop < xdata.num; loop++)
                {
                    SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%24u %6u %8u\n", xdata.fields[loop].width, xdata.fields[loop].offset, xdata.fields[loop].data);
                }
            }
            break;
        }
        case CTC_FIELD_KEY_PORT_CID:
        {
            ctc_xdata_t xdata;
            ctc_xdata_field_t fields[32];

            sal_memset(&key, 0, sizeof(key));
            sal_memset(&xdata,0, sizeof(ctc_xdata_t));
            sal_memset(fields,0, 32*sizeof(ctc_xdata_field_t));
            xdata.fields = fields;

            key.type = qid;
            key.ext_data = &xdata;
            sys_at_acl_get_flex_hash_field(lchip, &key, pe);
            if(xdata.num)
            {
                value[0] = fields[0].data;
                SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d\n", ifp_field_str[qid], value[0]);
            }
            break;
        }

        default:
            sal_memset(&key, 0, sizeof(key));
            key.type = qid;
            key.ext_data = value;
            sal_memset(value, 0, sizeof(value));
            sys_at_acl_get_flex_hash_field(lchip, &key, pe);
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "%-16s : %d\n", ifp_field_str[qid], key.data);
            break;
        }
    }

    return CTC_E_NONE;
}
#endif
int32
sys_tmm_acl_replace_block(uint8 lchip, ctc_acl_replace_t* p_replace)
{
    int32 ret = CTC_E_NONE;
    uint8  head_block_id;
    sys_acl_league_t* p_head_league;
    sys_acl_league_t* p_src_league;
    sys_acl_league_t* p_dest_league;
    sys_acl_block_t* p_src_block;
    sys_acl_block_t* p_dest_block;
    sys_acl_block_t* pb_tmp;
    sys_acl_lt_block_t *p_ltb_src = NULL;
    sys_acl_lt_block_t *p_ltb_dst = NULL;
    uint8 part;
    uint8 merge_size;
    uint8 block_base = p_replace->dir == CTC_INGRESS ? 0 : ACL_IGS_BLOCK_MAX_NUM;
    uint8 key_size = 0;
    sys_acl_entry_t*    pe;
    sys_stats_acl_change_t change_flow_stats = {0};
    uint32 cancel_bmp = 0;
    uint32 set_bmp = 0;
    uint8 blk_list[1];
    uint8 lt_merge_size;
    ctc_slistnode_t* node;
    sys_acl_group_t* pg;
    sys_acl_lt_block_t *pltb;
    ctc_slistnode_t* group_node;
    ctc_list_pointer_node_t* entry_node;
    sys_acl_block_t* p_hdr_block;
    uint32  old_key_id;
    uint32  old_act_id;
    uint32  old_hw_index;
    uint32  temp_id = 0;
    uint32  data    = 0;
    uint32  hw_table[CTC_BOTH_DIRECTION] = {DsProgrammingAclTcam0_t, DsEgrProgrammingAclTcam0_t};
    uint8   global_pp_en = SYS_ACL_IS_GLOBAL_PP_EN_LKUP(lchip, p_replace->dir, p_replace->src_lkup_level);

    p_src_league = &(p_usw_acl_master[lchip]->league[p_replace->src_lkup_level+block_base]);
    p_head_league = &(p_usw_acl_master[lchip]->league[p_src_league->hdr_block_id]);
    p_dest_league = &(p_usw_acl_master[lchip]->league[p_replace->dest_lkup_level+block_base]);
    p_src_block = &SYS_ACL_BLOCK(lchip, p_replace->src_lkup_level+block_base);
    p_dest_block = &SYS_ACL_BLOCK(lchip, p_replace->dest_lkup_level+block_base);
    merge_size = SYS_ACL_BLOCK(lchip, p_src_league->hdr_block_id).fpab.part_num;
    lt_merge_size = SYS_ACL_LT_BLOCK(lchip, p_src_league->hdr_block_id).fpab.part_num;
    p_ltb_src = &SYS_ACL_LT_BLOCK(lchip, p_replace->src_lkup_level+block_base);

    merge_size = merge_size < lt_merge_size ? lt_merge_size : merge_size;
    CTC_MAX_VALUE_CHECK((merge_size+p_replace->dest_lkup_level), p_replace->dir == CTC_INGRESS ? ACL_IGS_BLOCK_MAX_NUM : ACL_EGS_BLOCK_MAX_NUM);
    if(MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_ACL))
    {
        uint32 src_bitmap = 0;
        uint32 dst_bitmap = 0;
        uint32 global_pp_en_bmp = 0;

        global_pp_en_bmp = p_usw_acl_master[SYS_PP_BASE(lchip)]->global_pp_en_bmp[!!p_replace->dir];
        src_bitmap = (global_pp_en_bmp >> p_replace->src_lkup_level) & ((1 << merge_size)-1);
        dst_bitmap = (global_pp_en_bmp >> p_replace->dest_lkup_level) & ((1 << merge_size)-1);
        if(src_bitmap != dst_bitmap)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Global acl bmp is conflict,line:%d\n",__LINE__);
            return CTC_E_NOT_READY;
        }
    }

    if ( p_src_block[0].fpab.part || p_ltb_src[0].fpab.part || 
        (p_src_block[merge_size-1].fpab.part_num > 1 && p_src_block[merge_size-1].fpab.part != p_src_block[merge_size-1].fpab.part_num - 1 ) ||
        (p_ltb_src[merge_size-1].fpab.part_num > 1 && p_ltb_src[merge_size-1].fpab.part != p_ltb_src[merge_size-1].fpab.part_num - 1 ) )
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Invalid action priority used!\n");
        return CTC_E_INVALID_CONFIG;
    }
    part = (p_replace->src_lkup_level > p_replace->dest_lkup_level) ? (p_replace->src_lkup_level - p_replace->dest_lkup_level):(p_replace->dest_lkup_level - p_replace->src_lkup_level);
    if(part < merge_size)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "src lkup level(%u) and dest lkup level(%u) is overlap\n", p_replace->src_lkup_level, p_replace->dest_lkup_level);
        return CTC_E_INVALID_PARAM;
    }
    if(part < lt_merge_size)
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "lt block: src lkup level(%u) and dest lkup level(%u) is overlap\n", p_replace->src_lkup_level, p_replace->dest_lkup_level);
        return CTC_E_INVALID_PARAM;    
    }
    
    for (part = 0; part < merge_size; part++)
    {
        pb_tmp = &SYS_ACL_BLOCK(lchip, part + p_replace->dest_lkup_level+block_base);
        if(pb_tmp->block_status != SYS_ACL_BLOCK_STATUS_UNMERGE)
        {
            return CTC_E_INVALID_CONFIG;
        }
    }

    head_block_id = p_src_league->hdr_block_id;
    if(!CTC_IS_BIT_SET(p_head_league->lkup_level_bitmap, p_replace->src_lkup_level))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "league src lookup level (%u) was wrong\n", p_replace->src_lkup_level);
        return CTC_E_INVALID_PARAM;
    }

    cancel_bmp = 0;
    for (part=0; part<merge_size; part++)
    {
        cancel_bmp |= p_head_league[part].lkup_level_bitmap;
    }
    if(CTC_IS_BIT_SET(cancel_bmp, p_replace->dest_lkup_level))
    {
        SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "league dest lookup level (%u) was wrong\n", p_replace->dest_lkup_level);
        return CTC_E_INVALID_PARAM;
    }

    /*check no presel in the dest lookup level*/
    for (part=0; part<merge_size; part++)
    {
        pltb = &SYS_ACL_LT_BLOCK(lchip, p_replace->dest_lkup_level+part+block_base);
        if(pltb->fpab.entry_count != pltb->fpab.free_count && !p_usw_acl_master[lchip]->glb_presel_en)
        {
            SYS_ACL_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Exist presel entry in lookup level (%u)\n", p_replace->dest_lkup_level+part+block_base);
            return CTC_E_INVALID_PARAM;
        }
    }

    pb_tmp = p_dest_block;
    do
    {
        SYS_ACL_PART_LOOP(part, merge_size)
        {
            if(!BLOCK_EMPTY(pb_tmp, part))
            {
                return CTC_E_INVALID_PARAM;
            }
        }
        pb_tmp = pb_tmp->next;
    }while(pb_tmp);

    change_flow_stats.cb = _sys_usw_acl_change_stats_ptr_cb;
    p_hdr_block = &SYS_ACL_BLOCK(lchip, p_usw_acl_master[lchip]->league[p_replace->src_lkup_level+block_base].hdr_block_id);
    CTC_SLIST_LOOP(p_hdr_block->group_list, group_node)
    {
        pg = _ctc_container_of(group_node, sys_acl_group_t, head);
        CTC_LIST_POINTER_LOOP(entry_node ,&(pg->entry_list))
        {
            pe = _ctc_container_of(entry_node, sys_acl_entry_t, head);
            key_size = _sys_usw_acl_get_key_size(lchip, 1, pe, &part);
            SYS_ACL_GET_TBL_FUNC(lchip, pe, &old_key_id, &old_act_id, &old_hw_index);
            if( (old_key_id - hw_table[p_replace->dir]) < p_replace->src_lkup_level
                || (old_key_id - hw_table[p_replace->dir]) >= (p_replace->src_lkup_level + merge_size) )
            {
                continue;
            }
            SYS_ACL_GET_STATS_ID(temp_id, pe);
            if(0 != temp_id)
            {
                pb_tmp = &SYS_ACL_BLOCK(lchip, pe->group->group_info.block_id+block_base);
                change_flow_stats.stats_id = temp_id;
                ACL_MAP_FLOW_STATS_BLOCK_ID(pe, pb_tmp, change_flow_stats.new_block_id, key_size);
                change_flow_stats.new_block_id = change_flow_stats.new_block_id+p_replace->dest_lkup_level-p_replace->src_lkup_level;
                change_flow_stats.user_data = (void*)pe;
                CTC_ERROR_RETURN(sys_usw_flow_stats_change_acl_statsptr(lchip, &change_flow_stats));
            }
        }
    }
    
    /*copy hw table*/
    #if SDK_WORK_PLATFORM == 0
    if(drv_sdb_get_sdb_en(lchip) && p_usw_acl_master[SYS_PP_BASE(lchip)]->replace_block_dma_buf)
    {
        _sys_usw_acl_replace_dma_with_sdb_copy(lchip, p_replace, merge_size);
    }
    else if(p_usw_acl_master[SYS_PP_BASE(lchip)]->replace_block_dma_buf)
    {
        _sys_usw_acl_replace_dma_copy(lchip, p_replace, merge_size);
    }
    else
    #endif
    {
        _sys_usw_acl_replace_io_copy(lchip, p_replace);
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_GROUP, 1);
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_BLOCK, 1);
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_LT_BLOCK, 1);
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_PRESEL, 1);
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_FLEX_KEY, 1);

    cancel_bmp = 0;
    for (part=0; part<merge_size; part++)
    {
        CTC_BIT_SET(p_head_league[part].lkup_level_bitmap, p_replace->dest_lkup_level+part);
        CTC_BIT_SET(cancel_bmp, p_replace->src_lkup_level+part);
        p_dest_block[part].next = p_src_block[part].next;
        if(p_src_block[part].next)
        {
            p_src_block[part].next->prev = p_dest_block + part;
        }
        p_src_block[part].next = p_dest_block+part;
        p_dest_block[part].prev = p_src_block+part;
    }

    blk_list[0] = p_replace->dest_lkup_level + block_base;
    _sys_tmm_acl_set_league_hw(lchip, head_block_id, merge_size, 1, blk_list, SYS_ACL_LEAGUE_CONFIG_SET, global_pp_en);
    sys_tmm_acl_presel_with_league(lchip, head_block_id, p_head_league->lkup_level_bitmap, merge_size, TRUE, global_pp_en);
    
    /* update stats ram property */
    if (MCHIP_F_STATS(lchip)->set_ram_pp_property)
    {
        for (part=0; part<merge_size; part++)
        {
            CTC_BIT_SET(set_bmp, p_replace->dest_lkup_level+part);
        }
        
        MCHIP_F_STATS(lchip)->set_ram_pp_property(lchip, p_replace->dir, blk_list[0], set_bmp, global_pp_en);
    }
    
    if(global_pp_en)/* set global lkup bmp if src block is pp disable lkup */
    {
        p_usw_acl_master[lchip]->global_pp_en_bmp[p_replace->dir] |= set_bmp;
    }
    else/* unset global lkup bmp if src block is not pp disable lkup */
    {
         p_usw_acl_master[lchip]->global_pp_en_bmp[p_replace->dir] &= (~set_bmp);
    }
    SYS_USW_REGISTER_WB_SYNC_EN(lchip, CTC_FEATURE_ACL, SYS_WB_APPID_ACL_SUBID_MASTER, 1);

    if (CTC_IS_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_PORT))
    {
        CTC_ERROR_RETURN(sys_usw_port_api_set_acl_league_property(lchip, head_block_id, TRUE, 1, blk_list));
    }
    if (CTC_IS_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_VLAN))
    {
        CTC_ERROR_RETURN(sys_usw_vlan_set_acl_league_property(lchip, head_block_id, TRUE, 1, blk_list));
    }
    if (CTC_IS_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_L3IF))
    {
        CTC_ERROR_RETURN(sys_usw_l3if_set_acl_league_property(lchip, head_block_id, TRUE, 1, blk_list));
    }
            
    /*update block and league*/
    for (part=0; part<merge_size; part++)
    {
        /*update group info*/
        CTC_SLIST_LOOP(p_src_block[part].group_list, node)
        {
            pg = _ctc_container_of(node, sys_acl_group_t, head);
            pg->group_info.block_id = p_replace->dest_lkup_level+part;
        }

        /* if dest block have group of no entries, add these group to src block group_list */
        CTC_SLIST_LOOP_DEL(p_dest_block[part].group_list, node, group_node)
        {
            ctc_slist_add_tail(p_src_block[part].group_list, node);
        }
            
        mem_free(p_dest_block[part].fpab.entries);
        ctc_slist_free(p_dest_block[part].group_list);
        p_src_block[part].next = p_dest_block[part].next;
        sal_memcpy(p_dest_block+part, p_src_block+part, sizeof(sys_acl_block_t));
        p_dest_block[part].fpab.next = NULL;

        if(part>0)
        {
            p_dest_block[part-1].fpab.next = &p_dest_block[part].fpab;
        }
        p_dest_block[part].block_id = p_replace->dest_lkup_level+part+block_base;

        if(p_dest_block[part].next)
        {
            p_dest_block[part].next->prev = p_dest_block+part;
        }
        if(p_dest_block[part].prev)
        {
            p_dest_block[part].prev->next = p_dest_block+part;
        }

        sal_memcpy(p_dest_league+part, p_src_league+part, sizeof(sys_acl_league_t));
        p_dest_league[part].block_id = p_replace->dest_lkup_level+part+block_base;
        if(p_src_league != p_head_league)
        {
            CTC_BIT_UNSET(p_head_league[part].lkup_level_bitmap, p_replace->src_lkup_level+part);
            p_head_league[part].lkup_level_start[p_replace->dest_lkup_level+part] = p_head_league[part].lkup_level_start[p_replace->src_lkup_level+part];
            p_head_league[part].lkup_level_count[p_replace->dest_lkup_level+part] = p_head_league[part].lkup_level_count[p_replace->src_lkup_level+part];
            p_head_league[part].lkup_level_start[p_replace->src_lkup_level+part] = 0;
            p_head_league[part].lkup_level_count[p_replace->src_lkup_level+part] = 0;
        }
        else
        {
            CTC_BIT_UNSET(p_dest_league[part].lkup_level_bitmap, p_replace->src_lkup_level+part);
            p_dest_league[part].lkup_level_start[p_replace->dest_lkup_level+part] = p_dest_league[part].lkup_level_start[p_replace->src_lkup_level+part];
            p_dest_league[part].lkup_level_count[p_replace->dest_lkup_level+part] = p_dest_league[part].lkup_level_count[p_replace->src_lkup_level+part];
            p_dest_league[part].lkup_level_start[p_replace->src_lkup_level+part] = 0;
            p_dest_league[part].lkup_level_count[p_replace->src_lkup_level+part] = 0;

            p_dest_block[part].expand_blk_id = p_replace->dest_lkup_level+block_base;
            pb_tmp = p_dest_block+part;
            do
            {
                p_usw_acl_master[lchip]->league[pb_tmp->block_id].hdr_block_id = (pb_tmp->block_status==SYS_ACL_BLOCK_STATUS_UNMERGE) ? p_replace->dest_lkup_level+part+block_base:p_replace->dest_lkup_level+block_base;
                p_usw_acl_master[lchip]->league[pb_tmp->block_id].merged_to = p_replace->dest_lkup_level+part+block_base;
                pb_tmp = pb_tmp->next;
            }while(pb_tmp);
        }
        sys_usw_acl_fpa_block_init(lchip, p_replace->src_lkup_level+part+block_base);

        /*lt table copy*/
        p_ltb_src = &SYS_ACL_LT_BLOCK(lchip, p_replace->src_lkup_level+block_base+part);
        p_ltb_dst = &SYS_ACL_LT_BLOCK(lchip, p_replace->dest_lkup_level+block_base+part);
        if(!p_usw_acl_master[lchip]->glb_presel_en)
        {
            mem_free(p_ltb_dst->fpab.entries);
            sal_memcpy(p_ltb_dst, p_ltb_src, sizeof(sys_acl_lt_block_t));
            p_ltb_dst->block_id = p_replace->dest_lkup_level+block_base+part;
            _sys_tmm_acl_presel_fpa_block_init(lchip, p_replace->src_lkup_level+block_base+part);
        }
        else/* if glb_presel_en, do not re-init lt table data */
        {
            SWAPVAL(uint32, p_ltb_src->entry_valid, p_ltb_dst->entry_valid);
            SWAPVAL(uint32, p_ltb_src->is_key160, p_ltb_dst->is_key160);
        }
    }

    /*update flex key*/
    data = lchip << 24 | merge_size << 16 | (p_replace->dest_lkup_level+block_base) << 8 | (p_replace->src_lkup_level+block_base);
    ctc_vector_traverse2(SYS_ACL_DFT_FLEX_KEY_VEC(lchip), 0, (vector_traversal_fn2)_sys_tmm_acl_replace_update_flex_key, &data);
    ctc_vector_traverse2(SYS_ACL_FLEX_KEY_VEC(lchip), 0, (vector_traversal_fn2)_sys_tmm_acl_replace_update_flex_key, &data);

    /*update presel*/
    if (!p_usw_acl_master[lchip]->glb_presel_en)
    {
        sys_acl_lt_block_t* user_data[2];
        user_data[0] = &SYS_ACL_LT_BLOCK(lchip, p_replace->src_lkup_level+block_base);
        user_data[1] = &SYS_ACL_LT_BLOCK(lchip, p_replace->dest_lkup_level+block_base);
        ctc_vector_traverse(p_usw_acl_master[lchip]->presel_vec, _sys_tmm_acl_replace_update_presel_fn, (void*)&user_data);
    }

    /* disable port lookup on src_lkup_level & delete the src presel tcam */
    blk_list[0] = p_replace->src_lkup_level + block_base;
    if (CTC_IS_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_PORT))
    {
       CTC_ERROR_RETURN(sys_usw_port_set_acl_league_property(lchip, head_block_id, FALSE, 1, blk_list));
    }
    if (CTC_IS_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_VLAN))
    {
       CTC_ERROR_RETURN(sys_usw_vlan_set_acl_league_property(lchip, head_block_id, FALSE, 1, blk_list));
    }
    if (CTC_IS_BIT_SET(p_usw_acl_master[lchip]->league_update_bmp, SYS_ACL_LEAGUE_UPDATE_L3IF))
    {
       CTC_ERROR_RETURN(sys_usw_l3if_set_acl_league_property(lchip, head_block_id, FALSE, 1, blk_list));
    }
    sys_tmm_acl_presel_with_league(lchip, (p_src_league == p_head_league)?p_replace->dest_lkup_level+block_base:head_block_id, cancel_bmp, merge_size, FALSE, global_pp_en);

    /* delete hw table*/
    #if SDK_WORK_PLATFORM == 0
    if(drv_sdb_get_sdb_en(lchip) && p_usw_acl_master[SYS_PP_BASE(lchip)]->replace_block_dma_buf)
    {
        _sys_usw_acl_replace_dma_with_sdb_delete(lchip, p_replace, merge_size);
    }
    else if(p_usw_acl_master[SYS_PP_BASE(lchip)]->replace_block_dma_buf)
    {
        _sys_usw_acl_replace_dma_delete(lchip, p_replace, merge_size);
    }
    else
    #endif
    {
        _sys_usw_acl_replace_io_delete(lchip, p_replace);
    }

    /* config cancel league hw table*/
    blk_list[0] = p_replace->src_lkup_level+block_base;
    _sys_tmm_acl_set_league_hw(lchip, (p_src_league == p_head_league)?p_replace->dest_lkup_level+block_base:head_block_id, merge_size, 1, blk_list, SYS_ACL_LEAGUE_CONFIG_CANCEL, global_pp_en);

    return ret;
}

#endif

